When I click a button on a page (in IE browser), a new popup page opens. My attempts to get the window handle for this popup have failed. Here is my first attempt:
String baseWin = driver.getWindowHandle();
System.out.println(baseWin);
Sets = driver.getWindowHandles();
Iterator ite = s.iterator();
while ( ite.hasNext() ) {
String popUpHandle = ite.next();
if(!baseWin.equals(popUpHandle)) {
driver.switchTo().window(popUpHandle);
System.out.println(driver.switchTo().window(popUpHandle).getTitle());
This attempt prints only the base window's handle, and if the second print statement is placed outside the if() statement, but within the while() statement and after the if() statement, it simply outputs the base window's title. So the set of handles only seems to contain the base window handle.
Here is my second attempt:
String baseWin = driver.getWindowHandle();
System.out.println(baseWin);
ArrayList popUpWin = new ArrayList(driver.getWindowHandles());
popUpWin.remove(baseWin);
driver.switchTo().window(popUpWin.get(0));
System.out.println(driver.switchTo().window(popUpWin.get(0)));
This attempt returns an error, which says that the array popUpWin is empty, i.e. size == 0. So, the popup window's handle isn't being retrieved when I call driver.getWindowHandles() and only contains the base window's handle. Is this an IE issue? Is there a workaround? Or am I overlooking something in my code? (Note that I've neglected pauses in the code that I've included here, so I don't believe that is the issue.)
解决方案
There are 2 things you need to do:
Change the security setting in IE browser:
Open IE browser, click on "Internet Options" => "Security" => tick "Enable Protected Mode" for "Internet", "Local intranet", "Trusted sites" and "Restricted sites".
This gives IE driver the capability to get control of the new window handle, so that when you call
driver.getWindowHandles(); or driver.getWindowHandles().size();
you will get all the handles including the original window and the new windows. To be more accurate, you just need to set the security setting for all 4 domains to be the same which means you can uncheck "Enable Protected Mode" for all 4 domains but it is discouraged obviously.
After you call driver.switchTo().window(windowName);, you need to add ((JavascriptExecutor) driver).executeScript("window.focus();"); before the IE driver can perform any actions on the window.
This is because IE driver needs the window that it is working on to be at the foreground, this line helps the driver get the focus of the window so that it can perform any actions on window that you want.
The following is a complete sample:
String baseWin = driver.getWindowHandle();
//Some methods to open new window, e.g.
driver.findElementBy("home-button").click();
//loop through all open windows to find out the new window
for(String winHandle : driver.getWindowHandles()){
if(!winHandle.equals(baseWin)){
driver.switchTo().window(winHandle);
//your actions with the new window, e.g.
String newURL = driver.getCurrentUrl();
}
}
//switch back to the main window after your actions with the new window
driver.close();
driver.switchTo().window(baseWin);
//let the driver focus on the base window again to continue your testing
((JavascriptExecutor) driver).executeScript("window.focus();");