selenium webdriver switch window using title.

Solved 1 Answers 910 Views Selenium
0 1
Is there anyway I can swith to window using title in Selenium WebDriver?

1 Answer

Best answer

You may try using below function provided that you are handeling windows with different titles.


private String mainWindowsHandle; // Stores current window handle
public static boolean swithToWindow(WebDriver driver,String title){
 mainWindowsHandle = driver.getWindowHandle();
 Set<String> handles = driver.getWindowHandles(); // Gets all the available windows
 for(String handle : handles)
   driver.switchTo().window(handle); // switching back to each window in loop
   if(driver.getTitle().equals(title)) // Compare title and if title matches stop loop and return true
    return true; // We switched to window, so stop the loop and come out of funcation with positive response
 driver.switchTo().window(mainWindowsHandle); // Switch back to original window handle
 return false; // Return false as failed to find window with given title.



answered by (327 points)
selected by

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.

We have resolved 1.2k+ Software Testing queries!

Top Contributors