Abstract: Copyright Notice: This article for Bo Master original article, reproduced please indicate the source.
In the course of the page operation, sometimes clicking on a link will pop up a new window, when we need to operate the newly opened page, we need to switch the host to a new open window and then operate, such as the Essence of the blog Park Essence of the blog, each click on a page will open a browser window. Webdriver provides a switchTo (). The window () method can be implemented to switch between different Windows .
Method |
Description |
SwitchTo (). Window () |
Prototype |
Webdriver window (java.lang.String nameorhandle) |
Parameters |
Nameorhandle:webdriver the name of the window or handle returned; Acquired through Getwindowhandle (); |
Role |
Switch to the window with the given name/handle. |
Usage |
Driver.switchto (). window (Nameorhandle); |
Abnormal |
nosuchwindowexception-If the window is not found |
Getwindowhandle |
Prototype |
Java.lang. String getwindowhandle (); |
Role |
Returns an opaque handle that uniquely identifies this driver instance in this window. |
GetwindowhandleS |
Prototype |
Java.util. Set<java.lang.String> getwindowhandles () |
return value |
Set set, you can refer to the relevant methods of the set container to manipulate it; |
In the following test code, we log in to the blog park, open the essence of the blog Park page, then into the top two essence blog, and then respectively to the two blog "Comment Area", enter "Test ———— SwitchTo () method" Testing multi-window switch, but in order to maintain the environment in harmony, no comment submission operation ;
Packagecom. Seleniumlib.jase;Importjava.util.List;ImportJava.util.Set;Importorg.openqa.selenium.By;ImportOrg.openqa.selenium.WebDriver;Importorg.openqa.selenium.WebElement;ImportOrg.openqa.selenium.chrome.ChromeDriver; Public classSwitchTo { Public Static voidMain (String[]args)throwsinterruptedexception{Webdriver Driver; System.setproperty ("Webdriver.chrome.driver", "D:/selenium-java-3.5.3/chromedriver.exe");//Local storage path for Chromedriver drivesDriver =NewChromedriver (); /*Multi-Window switching ———— Testing the overall process: Open the Cnblogs Essence page, and then go to the top two essence blog, and then respectively to the two blog post "comment Area" (in order to maintain the environment in harmony, no comment submission operation)*/Driver.get ("Https://passport.cnblogs.com/user/signin?ReturnUrl=https%3A%2F%2Fwww.cnblogs.com%2F"); Driver.findelement (By.id ("Input1"). SendKeys ("Angels may not be present")); Blog Park login Name driver.findelement (by.id ("Input2")). SendKeys ("* * *");//Blog Park login PasswordDriver.findelement (By.id ("signin") . Click (); Thread.Sleep (2000);//need to wait a few s;Driver.get ("https://www.cnblogs.com/pick/");//jump to the "essence" page;String Jhwindowhandle = Driver.getwindowhandle ();//get the window handle of the essence pageSystem.out.println ("Jhwindowhandle:" +jhwindowhandle); //Open the top two blog posts in turn;list<webelement> elements = driver.findelements (By.classname ("Titlelnk"));//get all the Essentials blog posts; for(inti=0; I < 2;++i)//control the number of open posts{elements.get (i). Click (); //Open the "i+1" blog post;Thread.Sleep (2000); } //enter "Comments" on the two posts opened above but do not submit comments;set<string> Set = Driver.getwindowhandles ();//gets all open window handles, gets to three = Essence Home + 2 blog post; intSize = Set.size ();//get the number of open windows inti = 0; System.out.println ("The number of Windows opened by the test code:" +size); for(String windowhandles:set) {System.out.println ("windowhandle[" +i+ "] =" +windowhandles); Driver.switchto (). window (windowhandles); if(! Windowhandles.equals (Jhwindowhandle))//The non-essential homepage, which is one of two posts, can be "commented" in the comment area;{System.out.println ("Currenturl:" +Driver.getcurrenturl ()); Driver.findelement (By.id ("Tbcommentbody")). SendKeys ("Test ———— SwitchTo () method");//only input not submitted;//Thread.Sleep (+); } ++i; } System.out.println ("End Selenium"); //driver.close (); }}
Selenium_webdriver_ Multi-Window switching