power_settings_new

selenium webdriver switch window using title.

by
asked
0 votes
2.3k views
Is there anyway I can swith to window using title in Selenium WebDriver?

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.

1 Answer

answered by (327 points)
selected by
0 votes
 
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.
}

 

 

add
...