Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

0 like 0 dislike
6.5k views
in Selenium by
recategorized by
Is there anyway I can swith to window using title in Selenium WebDriver?

1 Answer

0 like 0 dislike
by The go-to Tester (327 points)
selected by
 
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.
}

 

 


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

...