How to handle popup windows in Selenium
Read Catalogue
Principle
In the code, through set<string> allwindowsid = Driver.getwindowhandles ();
To get a handle to all pop-up browsers, and then traverse through, using the Swithcto.window (Newwindow_handle) method. You can navigate to the new window.
HTML of the test page
<HTML><Head> <title>Common Web UI element operations, and API usage</title> <Scripttype= "Text/javascript"> functionOpen_win () {window.open ("http://www.cnblogs.com") } </Script></Head><Body> <form> <inputtype=buttonvalue= "open Window"onclick= "Open_win ()"> </form> </Div></Body></HTML>
Java Code
Public static void Testmultiplewindowstitle (Webdriver driver) throws Exception {String url= "e:\\stashfo Lder\\[email protected]\\stash\\tank-moneyproject\\selenium webdriver\\alluielement.html "; Driver.get (URL); Gets 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 (); Gets 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 (). Windows (PARENTWINDOWID); System.out.println ("Parentwindowid:" + driver.gettitle ()); }
Java Selenium (12) Operation Popup window