Skip to content

Instantly share code, notes, and snippets.

@angiejones
Created October 31, 2020 20:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save angiejones/0069465a3bfc8b0999f5f7d4412b8a5a to your computer and use it in GitHub Desktop.
Save angiejones/0069465a3bfc8b0999f5f7d4412b8a5a to your computer and use it in GitHub Desktop.
Window management in Selenium 4
public void windowsAndTabs() {
//Open initial window
driver.get("https://applitools.com/");
//Open new tab. If you want a new browser window, use WindowType.WINDOW
driver.switchTo().newWindow(WindowType.TAB);
driver.get("https://testautomationu.com");
//Switch between windows/tabs by using window handles
var windows = driver.getWindowHandles();
for(var window : windows){
driver.switchTo().window(window);
if("Automated Visual Testing with Visual AI".equals(driver.getTitle())){
//This is the tab I want in focus, so stop looping.
break;
}
}
System.out.println(driver.getTitle());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment