how to get the count of Multiple tabs in selenium code example
Example 1: multiple tabs in selenium
Selenium stays on one window. If you open a window and then 5 tabs popped open,
selenium is focused on the first window
If you are on a new window and you tell selenium to print an element on
the default window, it will still work even that user’s focus is on the
new window. Must switch to new window:
- Use windowHandle()
- Driver.getWindowHandle()
- Everytime Selenium opens a browser, it's going to give an index ID
for the page called Handles
- Returns the handle/id of current page (as a string)
- driver.switchTo().window(string handle)
- driver.getWindowHandles() for multiple windows
Returns a Set of window handles
- Switch using titles:
for(string handle: driver.getWindowHandles()){
driver.switchTo().Window(handle)
if(driver.getTitle().equals(targetTitle)
break;
Example 2: how to handle tabs in selenium
How do we handle windows/tabs?
- We have to switch driver's focus to different window to be able to
handle that one using window handles.
- windowHandle: is a unique alphanumerical string randomly generated
for each window/tab.
-> To get single window handle?
-If we want to get the currently open window's handle: driver.getWindowHandle();
- What is the return type of driver.getWindowHandle();
- String
-> How do we get all of the handles of the currently open windows?
- driver.getWindowHandles();
- Return type: Set<String>
syntax: driver.switchTo().window(WINDOWHANDLE);