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

415,581

Solution 1

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)

Solution 2

Just to add to the content ...

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

use driver.switchTo().defaultContent();

Solution 3

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

Solution 4

Surya, your way won't work, because of two reasons:

  1. you can't close driver during evaluation of test as it will loose focus, before switching to active element, and you'll get NoSuchWindowException.
  2. if test are run on ChromeDriver you`ll get not a window, but tab on click in your application. As SeleniumDriver can't act with tabs, only switchs between windows, it hangs on click where new tab is being opening, and crashes on timeout.

Solution 5

I use iterator and a while loop to store the various window handles and then switch back and forth.

//Click your link
driver.findElement(By.xpath("xpath")).click();
//Get all the window handles in a set
Set <String> handles =driver.getWindowHandles();
Iterator<String> it = handles.iterator();
//iterate through your windows
while (it.hasNext()){
String parent = it.next();
String newwin = it.next();
driver.switchTo().window(newwin);
//perform actions on new window
driver.close();
driver.switchTo().window(parent);
            }
Share:
415,581
Volodymyr  Prysiazhniuk
Author by

Volodymyr Prysiazhniuk

Updated on November 09, 2021

Comments

  • Volodymyr  Prysiazhniuk
    Volodymyr Prysiazhniuk over 2 years

    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.

  • Volodymyr  Prysiazhniuk
    Volodymyr Prysiazhniuk over 12 years
    thank you for suggestion, is there any way, how I can set window name mask? Like "WinName*", where * - any symbol.
  • Volodymyr  Prysiazhniuk
    Volodymyr Prysiazhniuk over 12 years
    My implicitly is 10 seconds, but in 2 sec's it gives me exception NoSuchWindowException.
  • Ben
    Ben over 10 years
    You can actually use the call for driver.close() since you are switching window handles on the driver and calling for the window that you are currently on to close. I use it regularly in my code when dealing with multiple windows. driver.Quit() is the call that will kill the test. In Chromedriver you can still call for getWindowHandles() to get your list of available windows. I cant think of a reason to use a tab in a web test.
  • paul
    paul about 10 years
    It is failing to switch to new tabs, however it performs clicks on desired new tab. Why?
  • Hugo
    Hugo over 8 years
    is there any reason this code may cause the test to become very slow? It seems that after this bit of code it takes about 10 minutes to perform any actions in the newwin
  • Ben
    Ben over 8 years
    @Elmue The original question stated that a click opens up a second window creating a multiple window handles. I can make the call driver.close(); and have my window that currently has focus close. I can then switch to any other active window of my choosing. driver.close() and driver.quit() do not share the same functionality. A call for driver.quit() will kill the active driver instance whereas close only kills the current window handle. Also, I have yet to see any call that solely opens a tab after a click is called when the javascript on the website creates a new window.
  • Elmue
    Elmue over 8 years
    I was wrong and deleted my comment. You can delete your answer too.
  • Ripon Al Wasim
    Ripon Al Wasim over 8 years
    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.CON‌​TROL + "w");
  • silver
    silver almost 7 years
    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.
  • Elmue
    Elmue almost 7 years
    @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.
  • silver
    silver almost 7 years
    @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.
  • Elmue
    Elmue almost 7 years
    The comment of Luke does not say anything new. "You cannot assume that the last window returned by getWindowHandles() will be the last one opened". This clearly says that this is a msidesign in Java. Why not report this as a bug so they fix it? How do I get the LAST OPENED window in Java then? At least this page has no usefull answer! It is simply a shame for Selenium (like a lot of other misdesigns). "A C# solution cannot be claimed to be correct." A very clever comment! C# works but it is not correct. Java does not work, but you defend the misdesign!
  • silver
    silver almost 7 years
    @Elmue That's a known behavior of the Java Set<T>, it's the LinkedHashSet<T> that retains insertion order. Seeing getWindowHandles() simply returns a Set<T>, it's a FACT it has no order. Getting the last opened window by chance doesn't mean that's what it actually does. This will become more important when you have more than just 2 windows open. Your solution may work in C#, but again, this is a Java question. Your answer is invalid for this thread.
  • Elmue
    Elmue almost 7 years
    Well, I see that Iam wasting my time here. You still did not understand. Why does Java code return a Set<T>? There are other containers in Java that can be used instead, which maintain the order. If Selenium folks fix this, it will work in C# AND in Java. Is this really so difficult to understand?
  • silver
    silver almost 7 years
    @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
    silver almost 7 years
    @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.
  • EPadronU
    EPadronU over 6 years
    Quick question: if the implementation is in fact a LinkedHashSet, shouldn't that means that the order is preserved?
  • joshmcode
    joshmcode over 6 years
    @silver You have to change the IE security settings and then I was able to make this work in IE. Take a look at this answer here: stackoverflow.com/a/40337065/3757322
  • Keith Tyler
    Keith Tyler almost 6 years
    I see this code used for this problem, but it looks to me like it will switch to all other windows and ultimately settle on the one that comes last in the getWindowHandles() iterator. But where does it say that getWindowHandles() iterator reliably returns handles in order of creation?
  • Ashok kumar Ganesan
    Ashok kumar Ganesan about 4 years
    How do we handle if I open a new browser throught another instance? Instead of clicking the button