webdriver switch between tabs code example
Example 1: 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);
Example 2: switch tab in selenium
psdbComponent.clickDocumentLink();
ArrayList<String> tabs2 = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs2.get(1));
driver.close();
driver.switchTo().window(tabs2.get(0));