If you encounter a new window after clicking a button or link, you will need to use the window switch method.
In this case, the first to open the Baidu search interface, and then use JS to open a Baidu press, and then through the Selenium provides a window switching method to switch windows
1 Packagecom.test.windows;2 3 ImportJava.util.Iterator;4 ImportJava.util.Set;5 6 Importorg.openqa.selenium.By;7 ImportOrg.openqa.selenium.JavascriptExecutor;8 ImportOrg.openqa.selenium.WebDriver;9 ImportOrg.openqa.selenium.firefox.FirefoxDriver;Ten One Public classWindowsswitch { A - Public Static voidMain (string[] args) { - theWebdriver Driver =Newfirefoxdriver (); -Driver.get ("http://www.baidu.com"); - driver.manage (). window (). Maximize (); - +String Searchhandle =Driver.getwindowhandle (); -SYSTEM.OUT.PRINTLN ("Baidu Search handle:" +searchhandle); + A //get the link to Baidu News, then use JS to open a new window atWaitTime (3000); -String href = driver.findelement (By.cssselector ("[Name=tj_trnews]")). getattribute ("href"); -SYSTEM.OUT.PRINTLN ("The link of news is:" +href); -Javascriptexecutor executor =(javascriptexecutor) driver; -Executor.executescript ("window.open ('" + href + ")"); - inWaitTime (3000); -Set<string> handles =driver.getwindowhandles (); toIterator Iterator =handles.iterator (); +Iterator Iterator2 =handles.iterator (); - the //Enter the Baidu News window and get the title verification *WaitTime (3000); $ while(Iterator.hasnext ()) {Panax NotoginsengString h =(String) Iterator.next (); - if(H! =searchhandle) { the driver.switchto (). Window (h); + if(Driver.gettitle (). Contains ("Baidu News"))) { ASYSTEM.OUT.PRINTLN ("Switch to News page successfully"); the Break; +}Else { - Continue; $ } $ } - - } the - //back to Baidu homepageWuyiWaitTime (3000); the /* - * can be switched directly using Searchhandle, or can be used to switch Wu * DRIVER.SWITCHTO (). window (searchhandle); - * if (Driver.gettitle (). Contains ("Baidu a Bit")) { About * SYSTEM.OUT.PRINTLN ("Switch to search page successfully"); $ * Driver.findelement (By.cssselector ("#kw")). SendKeys ( - * "switch successfully"); } - */ - while(Iterator2.hasnext ()) { AString h =(String) Iterator2.next (); + if(Searchhandle.equals (h)) { the driver.switchto (). Window (h); - if(Driver.gettitle (). Contains ("Baidu a Bit"))) { $SYSTEM.OUT.PRINTLN ("Switch to search page successfully"); theDriver.findelement (By.cssselector ("#kw")). SendKeys ("switch successfully"); the Break; the}Else { the Continue; - } in } the the } About theWaitTime (3000); the driver.quit (); the + } - the Static Public voidWaitTime (intTime ) {Bayi the Try { the Thread.Sleep (time); -}Catch(interruptedexception e) { - //TODO auto-generated Catch block the e.printstacktrace (); the } the } the -}
Operation Result:
Baidu Search handle: {57536d1e-6817-4127-83d6-6eb138f28e51}
The link of news is:http://news.baidu.com/
Switch to News page successfully
Switch to search page successfully
Selenium Test (Java)--Multi-window switch (13)