0

I'm using below code to switch into windows :

    Set<String> handle=driver.getWindowHandles();
    System.out.println(handle);
    Iterator<String> it=handle.iterator();

    String parent=it.next();
    System.out.println("parent window =" +parent);
    String child=it.next();

    System.out.println("second child window =" +child);
    driver.switchTo().window(child);
    driver.findElement(By.xpath("//*[@id=\"fixHeader\"]/tbody/tr[2]/td[4]/a/span")).click();

    Thread.sleep(3000);
    String child2=it.next();
    System.out.println("Third child window =" +child2);
    driver.switchTo().window(child2); 

But getting below exception :

Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"stylenumberH"}

  • getting Nosuchelement exception – Bharat Aug 24 '18 at 7:05
  • Welcome to SQA. Which line of your code is throwing the exception? – dzieciou Aug 24 '18 at 7:20
  • from string child 2=it.next() getting console result as parent window =CDwindow-32BE03A3EEB4936887DAFA09ACFE1569 second child window =CDwindow-A3D0A7BCC9DAD738BFB5C3BDCA2C97BB Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"stylenumberH"} – Bharat Aug 24 '18 at 8:05
  • 1
    It is not related to opening third child window. The exception is usually thrown by findElement() method when it cannot find a matching element in your page. This particular one show there is no element with id "stylenumberH". However, in your code there's no such selector, because you use only XPath selector. It looks like the code you pasted here is different than the one you executed :-) – dzieciou Aug 24 '18 at 8:34
  • @dziecio Getting Exception in thread "main" java.util.NoSuchElementException at java.util.LinkedHashMap$LinkedHashIterator.nextNode(Unknown Source) – Bharat Aug 24 '18 at 10:12
0

Used below code to switch into the windows :

//Get current window 
String currentWindow = driver.getWindowHandle();

//Get all opened windows list
ArrayList tabs = new ArrayList (driver.getWindowHandles());

// print the count of windows
System.out.println(tabs.size());

//Now you can switch into desired window in your case you want to switch into 3rd one so you have to pass the index 2 as it started from 0
driver.switchTo().window(tabs.get(2));

// now perform the click 
driver.findElement(By.xpath("//*[@id=\"fixHeader\"]/tbody/tr[2]/td[4]/a/span")).click();

Thread.sleep(3000);

//Switch back to original window
driver.switchTo().window(currentWindow);
  • hi getting indexoutofbound exception ,want to make consoler to switch to second child window.scenario is on click of parent window--->child window come---->then on click on child window--->another grand child window display – Bharat Aug 24 '18 at 10:05
  • can you share the site URL – NarendraR Aug 24 '18 at 10:33
  • first you have to switch into child window then click and then again switch into last window – NarendraR Aug 24 '18 at 10:35
  • Yes this is scenario.URL is use internally .vpn protected – Bharat Aug 24 '18 at 10:47
0

Use this below code. You will be able to switch to the new window.

    String Parent_Window = driver.getWindowHandle();
    Set<String> AllWindows = driver.getWindowHandles();
    for (String Child : AllWindows) {
        if (!Parent_Window.equalsIgnoreCase(Child)) {
                driver.switchTo().window(Child);
  • Try this code with your respective condition. This is working fine on 2 windows at a time. – Faizan Mamji Aug 28 '18 at 7:22
0

This function is for switching from 2nd window to 3rd window.

public static void switchTo_Third_Window(){

 Set<String> lastWindow = driver.getWindowHandles();
 try {
 int l = lastWindow.size() - 1;

 int inc = -1;
 for (String win : lastWindow) {

 inc++;

 if (inc == l) {
 driver.switchTo().window(win);

 driver.manage().window().maximize();

 System.out.println(driver.getTitle());

 }

 }
 }
 catch (Exception e)
 {


 }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.