在IE上使用Java中的Selenium WebDriver无法获得新的窗口句柄 [英] Unable to get new window handle with Selenium WebDriver in Java on IE

查看:572
本文介绍了在IE上使用Java中的Selenium WebDriver无法获得新的窗口句柄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我单击页面上的按钮(在IE浏览器中)时,将打开一个新的弹出页面.我尝试获取此弹出窗口的窗口句柄失败.这是我的第一次尝试:

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);
Set<String>s = driver.getWindowHandles();
Iterator<String> 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());

此尝试仅打印基本窗口的句柄,并且如果第二个print语句放置在if()语句之外,但在while()语句内以及if()语句之后,则仅输出基本窗口的标题.因此这组句柄似乎只包含基本窗口句柄.

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.

这是我的第二次尝试:

String baseWin = driver.getWindowHandle();
System.out.println(baseWin);
ArrayList<String> popUpWin = new ArrayList<String>(driver.getWindowHandles());
popUpWin.remove(baseWin);
driver.switchTo().window(popUpWin.get(0));
System.out.println(driver.switchTo().window(popUpWin.get(0)));

此尝试返回一个错误,该错误表明数组popUpWin为空,即size ==0.因此,当我调用driver.getWindowHandles()且仅包含基本窗口的句柄时,不会检索弹出窗口的句柄. .这是IE问题吗?有解决方法吗?还是我忽略了代码中的某些内容? (请注意,我忽略了此处包含的代码中的暂停,所以我不认为这是问题所在.)

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.)

推荐答案

您需要做两件事:

  1. 在IE浏览器中更改安全设置:

  1. Change the security setting in IE browser:

打开IE浏览器,单击"Internet选项" =>安全性" =>选中启用保护模式",用于"Internet",本地Intranet",受信任的站点"和受限制的站点".

Open IE browser, click on "Internet Options" => "Security" => tick "Enable Protected Mode" for "Internet", "Local intranet", "Trusted sites" and "Restricted sites".

这使IE驱动程序具有控制新窗口句柄的功能,以便在您调用时 driver.getWindowHandles();driver.getWindowHandles().size(); 您将获得所有手柄,包括原始窗口和新窗口.为了更加准确,您只需要将所有4个域的安全设置都设置为相同即可,这意味着您可以取消选中所有4个域的启用保护模式",但显然不建议这样做.

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.

调用driver.switchTo().window(windowName);后,需要先添加((JavascriptExecutor) driver).executeScript("window.focus();");,然后IE驱动程序才能在窗口上执行任何操作.

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.

这是因为IE驱动程序需要将其正在处理的窗口置于前台,因此此行可帮助驱动程序获得窗口的焦点,以便它可以在所需的窗口上执行任何操作.

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.

以下是完整的示例:

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

这篇关于在IE上使用Java中的Selenium WebDriver无法获得新的窗口句柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆