Categories
java new-window selenium selenium-webdriver webdriver

How to switch to the new browser window, which opens after click on the button?

95

I have situation, when click on button opens the new browser window with search results.

Is there any way to connect and focus to new opened browser window?

And work with it, then return back to original(first) window.

1

130

You can switch between windows as below:

// Store the current window handle
String winHandleBefore = driver.getWindowHandle();

// Perform the click operation that opens new window

// Switch to new window opened
for(String winHandle : driver.getWindowHandles()){
    driver.switchTo().window(winHandle);
}

// Perform the actions on new window

// Close the new window, if that window no more required
driver.close();

// Switch back to original browser (first window)
driver.switchTo().window(winHandleBefore);

// Continue with original browser (first window)

12

  • 6

    I can confirm accepted solution consistently works in Chrome but not in IE. The new window handle is not recognized in IE. @Elmue is incorrect because getWindowHandles() returns a Set and a Set does not guarantee ordering. The last element is not always the last window. I am surprised his comment gets a lot of upvotes.

    – silver

    Aug 13, 2017 at 10:13


  • @silver: I have no idea what you are talking about. In my code WindowHandles returns a ReadOnlyCollection<string> for which I can guarantee that the last entry is always the latest opened window. I tested that. If this works perfectly in C#, but is implemented wrongly in Java you should report that as a bug.

    – Elmue

    Aug 14, 2017 at 3:34


  • 2

    @Elmue This is a Java thread. Please read Luke’s (Selenium developer) response regarding getWindowHandles(). It is a known behavior (not bug) of the method and the Java Set<T> in particular. Therefore, switchTo().window(handles[handles.length-1]) is not guaranteed in Java. A C# solution cannot be claimed to be correct.

    – silver

    Aug 26, 2017 at 3:23


  • 6

    @Elmue For the 3rd (and final) time, your answer is false for Java as supported by docs and dev which can MISLEAD people. Post an answer where you say it’s for C#, no problem. Raise an issue card in GitHub for Java, go ahead. Simply your answer is not applicable for the language TAGGED in the thread and people may think otherwise. If you cannot make the distinction, your comprehension is lacking.

    – silver

    Aug 28, 2017 at 18:07

  • 3

    @Elmue By the way, you are also wrong in saying driver.close() closes both windows. It only closes the current. driver.quit() kills all instances. I can see somebody already pointed this out to you. Your comment is full of mistakes. Have a good day.

    – silver

    Aug 28, 2017 at 18:12


13

Just to add to the content …

To go back to the main window(default window) .

use driver.switchTo().defaultContent();

    12

    This script helps you to switch over from a Parent window to a Child window and back cntrl to Parent window

    String parentWindow = driver.getWindowHandle();
    Set<String> handles =  driver.getWindowHandles();
       for(String windowHandle  : handles)
           {
           if(!windowHandle.equals(parentWindow))
              {
              driver.switchTo().window(windowHandle);
             <!--Perform your operation here for new window-->
             driver.close(); //closing child window
             driver.switchTo().window(parentWindow); //cntrl to parent window
              }
           }
    

    1

    • 1

      driver.close() will work for new window. If it is new Tab then use the code to close the new tab: driver.findElement(By.cssSelector(“body”)).sendKeys(Keys.CONTROL + “w”);

      Feb 25, 2016 at 12:41