Selenium没有在IE中检测到第二个窗口
我的应用程序打开了一个单击按钮的新窗口,我需要在该窗口中执行一些操作。 但是selenium webdriver的响应getWindowHandles()方法只有一个窗口id。 如果在打开新窗口后调用getWindowHandles()有延迟,则会发生这种情况。 selenium存在已知问题。 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://"); 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 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
记录There is a known issue with selenium
,这些问题不是问题 ,而是处理InternetExplorer
Required Configuration
的组合。 如果不考虑这些设置, InternetExplorer
可能无法按预期运行。 以下各项对于演示InternetExplorer v11
正确行为至关重要:
- 对于IE 10及更高版本,必须禁用
Enhanced Protected Mode
。 此选项位于“Internet Options
对话框的“Advanced
选项卡中。 - 浏览器
Zoom Level
必须设置为100%,以便可以将本机鼠标事件设置为正确的坐标。 - 您必须在显示设置中将
Change the size of text, apps, and other items
设置为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浏览器窗口没有焦点,以及尝试将鼠标hover在元素上时,鼠标事件目前存在一些问题。 -
Browser Focus
:如果窗口没有Browser Focus
IE本身似乎不完全尊重我们发送IE浏览器窗口(WM_MOUSEDOWN和WM_MOUSEUP)的Windows消息。 -
您可以在
here
找到有关Native Events
和Browser Focus
的详细讨论。 -
现在,您必须通过
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);
-
根据
Best Programming
实践Thread.sleep(1000);
是一个巨大的否,因为它降低了Test Performance
-
现在,您知道
Browser Clients
滞后于WebDriver
实例,因此我们必须经常同步它们。 因此,在收集windowHandles之前,您必须按如下方式引导WebDriverWait
,您可以detailed discussion here
找到detailed discussion here
:WebElement popup= driver.findElement(By.name("popup")); popup.click(); new WebDriverWait(driver,5).until(ExpectedConditions.numberOfWindowsToBe(2)); Set
windowHandles = driver.getWindowHandles(); System.out.println(windowHandles);
更新
我可以从你的评论中看到:
"Enable Enhanced Protected Mode" is unchecked in IE options. – Renjith Jan 9 at 7:26
以下是@JimEvans关于Protected Mode settings and the Capabilities hack
感觉博客Protected Mode settings and the Capabilities hack
@JimEvans在一个清晰明确的术语中指出背景Protected Mode settings and the Capabilities hack
努力:
当首次引入重写的IE驱动程序时,决定它将强制执行其所需的保护模式设置,并在未正确设置时抛出exception。 与IE的几乎所有其他设置一样,保护模式设置存储在Windows注册表中,并在实例化浏览器时进行检查。 但是,一些误入歧途的IT部门使开发人员和测试人员无法在他们的计算机上设置最基本的设置。
对于那些无法设置IE设置的人来说,驱动程序需要一种解决方法,因为他们的机器被过度锁定了。 这就是function设置的用途。 它只是绕过了注册表检查。 但是,使用该function并不能解决潜在的问题。 如果跨越保护模式边界,则可能导致非常意外的行为,包括挂起 , 元素位置不起作用以及未传播的点击 。 为了帮助警告人们这个潜在的问题,该function被赋予了一些可怕的名字,如
Java
INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS
和.NET
IntroduceInstabilityByIgnoringProtectedModeSettings
。 我们真的认为告诉用户使用此设置会在代码中引入潜在的不良后果会阻碍其使用,但事实certificate并非如此。如果您能够设置IE的保护模式设置,并且您仍在使用该function,那么您将面临代码稳定性的风险。 不要这样做。 设置设置。 这并不难。
以下是设置Protected Mode settings
:
- 以下是关于
Selenium IEServerDriver not finding new windows for IE9
另一个讨论,其中解决方案是开启兼容模式
窗口处理问题,主要是因为保护模式设置。 为所有区域启用保护模式或为所有区域禁用它并尝试。
不知道什么是Set,但我测试了下面的代码
while (true) { int qw = ololo.WindowHandles.Count; string[] wh = ololo.WindowHandles.ToArray(); ololo.FindElement(By.LinkText("Помощь")).Click(); Thread.Sleep(1000); }
它运作得很好。