栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

IE上的Java中无法使用Selenium WebDriver获取新的窗口句柄

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

IE上的Java中无法使用Selenium WebDriver获取新的窗口句柄

您需要做两件事:

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

打开IE浏览器,单击“ Internet选项” =>“安全” =>选中“启用保护模式”,以表示“
Internet”,“本地Intranet”,“受信任的站点”和“受限制的站点”。

这使IE驱动程序能够控制新窗口的句柄,以便在调用

driver.getWindowHandles();
driver.getWindowHandles().size();

时将获得所有句柄,包括原始窗口和新窗口。为了更加准确,您只需要将所有4个域的安全设置都设置为相同即可,这意味着您可以取消选中所有4个域的“启用保护模式”,但是显然不建议这样做。

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

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

以下是一个完整的示例:

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/399549.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号