java – Selenium没有检测到IE中的第二个窗口

前端之家收集整理的这篇文章主要介绍了java – Selenium没有检测到IE中的第二个窗口前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的应用程序打开一个单击按钮的新窗口,我需要在该窗口中执行一些操作.但是selenium webdriver的响应getWindowHandles()方法只有一个窗口id.如果在打开新窗口后调用getWindowHandles()有延迟,则会发生这种情况.硒存在已知问题.
https://github.com/SeleniumHQ/selenium/wiki/InternetExplorerDriver#required-configuration

但即使是解决方案也不适用于我.

代码如下

DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
RemoteWebDriver driver = new
        RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),capabilities);

driver.get("https://<url>");

WebElement userName = driver.findElement(By.name("usr_name"));
userName.sendKeys("ABCD");

WebElement password = driver.findElement(By.name("usr_password"));
password.sendKeys("password");

WebElement login = driver.findElement(By.name("OK"));
login.click();  


WebElement popup= driver.findElement(By.name("popup"));
popup.click();      

Thread.sleep(1000);

Set<String> windowHandles = driver.getWindowHandles();      
System.out.println(windowHandles);

Set“windowHandles”将只返回一个窗口:

"[fcdad457-9090-4dfd-8da1-acb9d6f73f74]"

但如果我取消睡眠.它将返回两个窗口ID:

[90cc6006-0679-450c-a5b3-6602bcb41a16,7211bbfd-2616-4460-97e7-56c0e632c3bb]

我无法移除睡眠,因为这只是一个示例程序,在实际应用程序中,它们之间会有一些延迟.请让我知道你的想法.这个问题仅适用于IE11.

蓝屏 – 主页;
灰色屏幕 – 弹出窗口

解决方法

在处理InternetExplorer时,您需要注意以下几点:

正如您所提到的,github中记录了selenium的已知问题,这些问题不是问题,而是处理InternetExplorer时的Required Configuration组合.如果不考虑这些设置,InternetExplorer可能不会按预期运行.以下各项对于演示InternetExplorer v11的正确行为至关重要:

>对于IE 10及更高版本,必须禁用增强保护模式.此选项位于“Internet选项”对话框的“高级”选项卡中.
>浏览器缩放级别必须设置为100%,以便可以将本机鼠标事件设置为正确的坐标.
>您必须在显示设置中将文本,应用和其他项目的大小更改为100%.
>对于IE 11,您需要在目标计算机上设置一个注册表项,以便驱动程序可以维护与其创建的Internet Explorer实例的连接.

For 32-bit Windows installations,the key you have to look in the registry is : 
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BFCACHE

For 64-bit Windows installations,the key is :
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BFCACHE

The FEATURE_BFCACHE subkey may or may not be present,and should be created if it is not present.

> Native Events:使用本机事件的优势在于它不依赖于JavaScript沙箱,并确保在浏览器中正确传播JavaScript事件.但是,当IE浏览器窗口没有焦点,以及尝试将鼠标悬停在元素上时,鼠标事件目前存在一些问题.
> Browser Focus:如果窗口没有焦点,IE本身似乎不完全尊重我们发送IE浏览器窗口(WM_MOUSEDOWN和WM_MOUSEUP)的Windows消息.
>您可以找到有关Native Events和Browser Focus here的详细讨论.
>现在,您必须通过DesiredCapabilities类配置所有这些参数,如下所示:

DesiredCapabilities cap = DesiredCapabilities.internetExplorer();
cap.setCapability("ignoreProtectedModeSettings",1);
cap.setCapability("IntroduceInstabilityByIgnoringProtectedModeSettings",true);
cap.setCapability("nativeEvents",true);
cap.setCapability("browserFocus",true);
cap.setCapability("ignoreZoomSetting",true);
cap.setCapability("requireWindowFocus","true");
cap.setCapability("INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS",true);

>根据最佳编程实践Thread.sleep(1000);是一个巨大的否,因为它降低了测试性能
>现在,您知道浏览器客户端滞后于WebDriver实例,因此我们必须经常同步它们.因此,在收集windowHandles之前,您必须按如下方式引导WebDriverWait,您可以在其中找到detailed discussion here

WebElement popup= driver.findElement(By.name("popup"));
popup.click();
new WebDriverWait(driver,5).until(ExpectedConditions.numberOfWindowsToBe(2));
Set<String> windowHandles = driver.getWindowHandles();      
System.out.println(windowHandles);

更新

我可以从你的评论中看到:

"Enable Enhanced Protected Mode" is unchecked in IE options. – Renjith Jan 9 at 7:26

以下是@JimEvans sensetional博客Protected Mode settings and the Capabilities hack发表的文章,其中@JimEvans以清晰明确的术语指出了背景:

When the rewritten IE driver was first introduced,it was decided that it would enforce its required Protected Mode settings,and throw an exception if they were not properly set. Protected Mode settings,like almost all other settings of IE,are stored in the Windows registry,and are checked when the browser is instantiated. However,some misguided IT departments make it impossible for developers and testers to set even the most basic settings on their machines.

The driver needed a workaround for people who couldn’t set those IE settings because their machine was overly locked down. That’s what the capability setting is intended to be used for. It simply bypasses the registry check. Using the capability doesn’t solve the underlying problem though. If a Protected Mode boundary is crossed,very unexpected behavior including hangs,element location not working,and clicks not being propagated,could result. To help warn people of this potential problem,the capability was given big scary-sounding names like INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS in Java and IntroduceInstabilityByIgnoringProtectedModeSettings in .NET. We really thought that telling the user that using this setting would introduce potential badness in their code would discourage its use,but it turned out not to be so.

If you are able to set the Protected Mode settings of IE,and you are still using the capability you are risking the stability of your code. Don’t do it. Set the settings. It’s not that hard.

以下是设置保护模式设置的方法

>这是Selenium IEServerDriver not finding new windows for IE9的另一个讨论,其中解决方案是打开兼容模式

原文链接:https://www.f2er.com/java/121030.html

猜你在找的Java相关文章