Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

selenium: is the first window handle always the main window?

Is the first window guaranteed to be the main window? Or is the order random and inconsistent?

for (String handle : driver.getWindowHandles()) {
    driver.switchTo().window(handle);
}

I can't find any information about the order of the window handles, just a way to iterate thorugh them all. I am assuming that the first in the list of window handles is going to be the main window.

like image 816
user299709 Avatar asked Dec 24 '14 06:12

user299709


People also ask

Can Selenium handle multiple windows?

The window handle in Selenium helps in handling multiple windows and child windows. Each browser will have a unique window handle value with which we can uniquely identify it.

What is window handle in Selenium?

The getWindowHandles method is used to store all the opened window handles in the Set data structure. The getWindowHandle method is used to store the window handle of the browser window in focus.

How do I switch between window handles in Selenium?

Iterate through child windows. Get the handles of all the windows that are currently open using the command: Set<String> allWindowHandles = driver. getWindowHandles(); which returns the set of handles. Use the SwitchTo command to switch to the desired window and also pass the URL of the web page.

How does Selenium handle 4th window?

Opening an URL in a new window or new tab is one of the features provided by Selenium 4. In Selenium 3, we can achieve this by performing switch operation using the WindowHandle method. In Selenium 4, this is going to change by using the new API newWindow.


1 Answers

According to the current WebDriver API Specification:

6.3 Iterating Over Windows

getWindowHandles

An array containing a window handle for every open window in this session. This array of returned strings must contain a handle for every window associated with the browser session and no others. For each returned window handle the javascript expression "window.top.closed" (or equivalent) must evaluate to false at the time the command is being executed.

The ordering of the array elements is not defined, but may be determined by iterating over each top level browser window and returning the tabs within that window before iterating over the tabs of the next top level browser window.

In short, no - there is no guaranteed order.

like image 68
alecxe Avatar answered Sep 25 '22 21:09

alecxe