java在ie上,在IE上使用Java中的Selenium WebDriver无法获得新的窗口句柄

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();");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值