Selenium Webdriver Learning (vi)------------How to get pop-up windows
Getting a pop-up window inside the selenium 1.X is a hassle, especially when new windows don't have IDs or name. At that time also sorted out the treatment of several methods, see: http://seleniumcn.cn/read.php?tid=791. It is relatively simple to get a new open window in the Selenium webdriver, which does not concern the ID, name, etc. of the newly opened window. Take the following HTML as an example:
test.html
The following code shows how to get a new window that pops up
Import Java.util.Iterator;
Import Java.util.Set;
Import Org.openqa.selenium.By;
Import Org.openqa.selenium.WebDriver;
Import Org.openqa.selenium.firefox.FirefoxDriver;
public class Popupwindowtest {
/**
* @author GONGJF
*
/public static void main (string[] args) {
System.setproperty ("Webdriver.firefox.bin", "D:\\Program Files\\mozilla firefox\\firefox.exe");
Webdriver dr = new Firefoxdriver ();
String url = "\\Your\\Path\\to\\main.html";
Dr.get (URL);
Dr.findelement (By.id ("Wuyi")). Click ();
Gets the handle
String Currentwindow = Dr.getwindowhandle () of the current window
; Gets the handle of all windows
set<string> handles = Dr.getwindowhandles ();
Iterator<string> it = Handles.iterator ();
while (It.hasnext ()) {
String handle = It.next ();
if (currentwindow.equals (handle)) continue;
Webdriver window = Dr.switchto (). window (handle);
System.out.println ("Title,url =" +window.gettitle () + ", +window.getcurrenturl ());}}}
Output results:
Title,url = 51.com Real person pairing play game, http://www.51.com/
The key to capturing or locating a pop-up window is to get a handle to the pop-up window. (Handle, my understanding is a browser window of a unique identification, remember to play the "Key Wizard" also have this kind of play. )
In the code above, use the WindowHandle method to get the handle of the current browser window, use the Windowhandles method to get all the handles of the pop-up browser window, and then get the handle to the new window by excluding the current handle.
After fetching the handle of the new pop-up window, use the Switchto.window (Newwindow_handle) method to pass the handle of the new window as a parameter to capture the new window.
If you want to go back to the previous window positioning element, then call the Switch_to.window Method 1 times and pass in the handle of the previous window to achieve the goal.
----------------------------------------------------------June 20, 2102------------------------------------------------------
PS: There are some problems finding the code in the while today. By the original:
while (It.hasnext ()) {
if (Currentwindow = = It.next ()) continue;
Webdriver window = Dr.switchto (). Window (It.next ());
System.out.println ("Title,url =" +window.gettitle () + "," +window.getcurrenturl ());
}
Change to:
while (It.hasnext ()) {
String handle = It.next ();
if (currentwindow.equals (handle)) continue;
Webdriver window = Dr.switchto (). window (handle);
System.out.println ("Title,url =" +window.gettitle () + "," +window.getcurrenturl ());
}
Reason for change:
Inside the loop there are two times it.next, take one more.