Skip to content

Instantly share code, notes, and snippets.

@ShamaUgale
Last active March 12, 2021 15:48
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 ShamaUgale/827a77b279322dcdd6c6395605aabcb0 to your computer and use it in GitHub Desktop.
Save ShamaUgale/827a77b279322dcdd6c6395605aabcb0 to your computer and use it in GitHub Desktop.
import org.openqa.selenium.WindowType;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.IOException;
import java.util.Iterator;
import java.util.Set;
/*
This is an example to demonstarte the usage of newWindow() method in Selenium 4
New window or tabbed windows can be created and used in the single session and switch between them with
switchTo().window() method
*/
public class WindowsTabsExample {
final static String PROJECT_PATH = System.getProperty("user.dir");
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", PROJECT_PATH+ "/src/main/resources/chromedriver");
ChromeDriver driver = new ChromeDriver();
driver.get("https://www.google.com/");
// open a new window
driver.switchTo().newWindow(WindowType.WINDOW);
//navigate to a URL on newly opened window
driver.navigate().to("https://www.youtube.com/");
// get the window ids to switch between them
Set<String> windows = driver.getWindowHandles();
Iterator<String> it = windows.iterator();
String googleWindow = it.next();
String youtubeWindow = it.next();
System.out.println(driver.getTitle());
// switch the control to the google window
driver.switchTo().window(googleWindow);
System.out.println(driver.getTitle());
// open a new tabbed window
driver.switchTo().newWindow(WindowType.TAB);
//navigate to a URL on newly opened tabbed window
driver.navigate().to("https://www.gmail.com/");
System.out.println(driver.getTitle());
driver.switchTo().window(youtubeWindow);
System.out.println(driver.getTitle());
driver.quit();
}
}
@evanhwolf
Copy link

Is WindowType still supported?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment