How to handle popups in selenium
Reading Contents
principle
In the code, use Set<String> allWindowsId = driver.getWindowHandles();
To get all the pop-up browser handles, then iterate over, use the swethcto.window(newwindow_handle) method. You can locate a new window
Test the HTML of the page
<html><head> <title>Common web ui element operations, and API usage</title> <script type="text/javascript"> function open_win() { window.open("http://www.cnblogs.com") } </script></head><body> <form> <input type=button value="Open window" onclick="open_win()"> </form> </div></body></html>Java code
public static void testMultipleWindowsTitle(WebDriver driver) throws Exception { String url="E://StashFolder//[email protected]//Stash//Tank-MoneyProject//Selenium Webdriver//AllUIElement.html"; driver.get(url); // Get the handle of the current window String parentWindowId = driver.getWindowHandle(); System.out.println("driver.getTitle(): " + driver.getTitle()); WebElement button = driver.findElement(By.xpath("//input[@value='Open Window']")); button.click(); Set<String> allWindowsId = driver.getWindowHandles(); // Get all open window handles for (String windowId : allWindowsId) { if (driver.switchTo().window(windowId).getTitle().contains("Blog Park")) { driver.switchTo().window(windowId); break; } } System.out.println("driver.getTitle(): " + driver.getTitle()); // Switch back to the original parent window driver.switchTo().window(parentWindowId); System.out.println("parentWindowId: " + driver.getTitle()); }The above is an example of Java selenium operation pop-up window. We will continue to organize relevant information in the future. Thank you for your support for this site!