0

I am trying to handle multiple browser windows using selenium webdriver, but getting java.util.NoSuchElementException. A week ago this code was working fine but now getting issue in the code while trying to switch to third window.

The issue I am getting on line String Third_window = iterate.next();

Note: This code was working fine properly a week ago.

  public class Firefox {
        public static WebDriver driver;

        public static void main(String[] args) throws InterruptedException {
            // TODO Auto-generated method stub
            System.setProperty("webdriver.gecko.driver",
                    "C:\\Users\\singhais\\Documents\\Selenium Prerequisites\\geckodriver.exe");
            driver = new FirefoxDriver();
            driver.get("https://www.hdfcbank.com/");
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

            // First window
            System.out.println("GettingID for first Window");
            Set<String> win = driver.getWindowHandles();
            Iterator<String> iterate = win.iterator();

            String first_window = iterate.next();
            System.out.println(first_window);
            driver.findElement(By.xpath("//*[@id='element2']/div[1]/div[3]/div[2]/a/img")).click();

            // second window
            System.out.println("GettingID for second Window");
            win = driver.getWindowHandles();
            iterate = win.iterator();

            first_window = iterate.next();
            System.out.println(first_window);
            System.out.println(driver.getTitle());
            String second_window = iterate.next();
            System.out.println(second_window);
            driver.switchTo().window(second_window);
            Thread.sleep(10000);
            System.out.println(driver.getTitle());

            driver.findElement(By.xpath("//*[@id='wrapp']/div[2]/div[3]/div[1]/div/div[2]/ul/li[1]/a")).click();

            // ThirWindow
            System.out.println("GettingID for third Window");
            win = driver.getWindowHandles();
            iterate = win.iterator();
            first_window = iterate.next();
            System.out.println(first_window);
            second_window = iterate.next();
            System.out.println(second_window);
            String Third_window = iterate.next();
            System.out.println(Third_window);
            driver.switchTo().window(Third_window);
            Thread.sleep(10000);
            System.out.println(driver.getTitle());

            Thread.sleep(3000);
            driver.close();

            driver.switchTo().window(second_window);

            Thread.sleep(3000);
            driver.close();

            driver.switchTo().window(first_window);
            driver.findElement(By.xpath("html/body/div[1]/div[1]/div[1]/div/div[2]/ul/li[2]/a")).click();

            Thread.sleep(6000);
            driver.close();

        }

    }

This code is not duplicate of the problem mentioned in given link below. In my issue I am getting NoSuchElementException while trying to navigate to last window.

Shan
  • 69
  • 1
  • 3
  • 11
  • Possible duplicate of [Switching into new window using selenium after button click](https://stackoverflow.com/questions/47082900/switching-into-new-window-using-selenium-after-button-click) – undetected Selenium Nov 23 '17 at 15:23
  • @DebanjanB No, this issue is not duplicate. In my case I am getting NoSuchElement exception while trying to switch to last window. – Shan Nov 23 '17 at 16:09
  • can you please post your full error console log – Zakaria Shahed Nov 23 '17 at 16:34
  • @zsbappa, Here is the full console log `Exception in thread "main" java.util.NoSuchElementException at java.util.LinkedHashMap$LinkedHashIterator.nextNode(Unknown Source) at java.util.LinkedHashMap$LinkedKeyIterator.next(Unknown Source) at hdfcbank.Firefox.main(Firefox.java:56)` – Shan Nov 23 '17 at 16:44

1 Answers1

1

Using Thread.sleep(3000); Your program works fine for me.I just wait 3s for getting next windows

        System.out.println("GettingID for third Window");
        Thread.sleep(3000);
        win = driver.getWindowHandles();
        iterate = win.iterator();

Please check and let me know

Zakaria Shahed
  • 2,589
  • 6
  • 23
  • 52
  • Thanks @zsbappa for your opinion on this, but I have latest gecko driver. Firefox.java is my class name, i think you got confused with this. – Shan Nov 23 '17 at 18:01
  • Many Thanks, it is working fine. How did you come to this solution, I mean what you have thought. I have not thought about the wait because previously it was working fine. – Shan Nov 24 '17 at 16:20
  • It happens when everything perfectly works but elements not visible..Thanks if you need anything more feel free knock me anytime – Zakaria Shahed Nov 24 '17 at 16:23