I'm using below code to switch into windows :

    Set<String> handle=driver.getWindowHandles();
    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);

    String child2=it.next();
    System.out.println("Third child 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
    Commented Aug 24, 2018 at 7:05
  • Welcome to SQA. Which line of your code is throwing the exception?
    – dzieciou
    Commented Aug 24, 2018 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
    Commented Aug 24, 2018 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
    Commented Aug 24, 2018 at 8:34
  • @dziecio Getting Exception in thread "main" java.util.NoSuchElementException at java.util.LinkedHashMap$LinkedHashIterator.nextNode(Unknown Source)
    – Bharat
    Commented Aug 24, 2018 at 10:12

3 Answers 3


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) {


 if (inc == l) {




 catch (Exception e)




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

//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

// now perform the click 


//Switch back to original window
  • 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
    Commented Aug 24, 2018 at 10:05
  • can you share the site URL
    – NarendraR
    Commented Aug 24, 2018 at 10:33
  • first you have to switch into child window then click and then again switch into last window
    – NarendraR
    Commented Aug 24, 2018 at 10:35
  • Yes this is scenario.URL is use internally .vpn protected
    – Bharat
    Commented Aug 24, 2018 at 10:47

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)) {
  • Try this code with your respective condition. This is working fine on 2 windows at a time. Commented Aug 28, 2018 at 7:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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