硒未检测到IE中的第二个窗口 [英] Selenium not detecting the second window in IE

查看:78
本文介绍了硒未检测到IE中的第二个窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序在单击按钮时打开了一个新窗口,我需要在该窗口中执行一些操作.但是,Selenium Webdriver的响应getWindowHandles()方法中只有一个窗口ID.特别是在打开新窗口后调用getWindowHandles()时出现延迟的情况.硒存在一个已知问题. https://github.com/SeleniumHQ/selenium/wiki/InternetExplorerDriver#required-configuration

My application opens up a new window on clicking a button and i need to perform some actions in that window. But the response getWindowHandles() method of selenium webdriver has only one window id in it. This happens especially if there is a delay in calling the getWindowHandles() after opening the new window. There is a known issue with selenium. https://github.com/SeleniumHQ/selenium/wiki/InternetExplorerDriver#required-configuration

但是,即使是这样的解决方案,对我也不起作用.

But even the solution for that is not working for me.

代码如下

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 "将仅返回一个窗口:

The Set "windowHandles" will return only one window :

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

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

But if i remove the sleep. it will return two window ids :

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

我不能删除睡眠,因为这只是一个示例程序,而在实际应用程序中,这之间会有一些延迟.请让我知道您的想法.此问题仅适用于IE11.

I cannot remove the sleep as this is just a sample program and in the real application there will be some delay in between. Please let me know your thoughts.This issue is only for IE11.

蓝屏-主页; 灰屏-弹出窗口

Blue screen - Home Page; Grey Screen - Popup

推荐答案

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

There a couple of things which you have to take care while dealing with InternetExplorer as follows :

正如您提到的 GitHub 中记录的There is a known issue with selenium一样,这些不是问题,而是

As you mentioned There is a known issue with selenium documented in GitHub, these are not issues as such but is the combined set of Required Configuration while dealing with InternetExplorer. Without taking care of these settings InternetExplorer may not behave as per expectation. The following items are critical to demonstrate proper behavior of 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实例的连接.

  • Enhanced Protected Mode must be disabled for IE 10 and higher. This option is found in the Advanced tab of the Internet Options dialog.
  • The browser Zoom Level must be set to 100% so that the native mouse events can be set to the correct coordinates.
  • You have to set Change the size of text, apps, and other items to 100% in display settings.
  • For IE 11, you will need to set a registry entry on the target computer so that the driver can maintain a connection to the instance of Internet Explorer it creates.

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浏览器窗口没有焦点并且试图将鼠标悬停在元素上时,鼠标事件当前存在一些问题.

  • Native Events : The advantage of using native events is that it does not rely on the JavaScript sandbox, and it ensures proper JavaScript event propagation within the browser. However, there are currently some issues with mouse events when the IE browser window does not have focus, and when attempting to hover over elements.

    Browser Focus :如果窗口没有焦点,IE本身似乎并不完全尊重我们向IE浏览器窗口发送的Windows消息(WM_MOUSEDOWN和WM_MOUSEUP).

    Browser Focus : IE itself appears to not fully respect the Windows messages we send the IE browser window (WM_MOUSEDOWN and WM_MOUSEUP) if the window doesn't have the focus.

    您可以在 Native Events Browser Focus 现在,您必须通过 DesiredCapabilities 类来配置所有这些参数,如下所示:

    Now, you have to configure all these parameters through DesiredCapabilities Class as follows :

    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

  • As per Best Programming practices Thread.sleep(1000); is a huge No as it degrades the Test Performance

    现在,您已经知道Browser Clients落后于WebDriver实例,因此我们必须经常对其进行同步.因此,在收集 windowHandles 之前,您必须按照以下步骤诱导WebDriverWait,为此您可以找到

    Now, as you are aware that the Browser Clients lags the WebDriver instance so we have to often sync up them. So before you collect the windowHandles you have to induce WebDriverWait as follows for which you can find a 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);
    

  • 我可以从您的评论中看到:

    I can see from your comments :

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

    这是@JimEvans感官博客在 Protected Mode settings and the Capabilities hack ,其中@JimEvans用清晰明确的术语来说明上下文:

    Here is the exert from @JimEvans sensetional blog on Protected Mode settings and the Capabilities hack where @JimEvans nails the context in a clear and unambiguous term :

    首次引入重写的IE驱动程序时,决定将强制执行其必需的保护模式设置,如果未正确设置它们,则会引发异常.与几乎所有其他IE设置一样,保护模式设置也存储在Windows注册表中,并在实例化浏览器时进行检查.但是,一些误导的IT部门使开发人员和测试人员甚至无法在其计算机上设置最基本的设置.

    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.

    对于无法设置这些IE设置的人,该驱动程序需要一种解决方法,因为他们的计算机被过度锁定.这就是打算使用的功能设置.它只是绕过注册表检查.但是,使用该功能并不能解决潜在的问题.如果越过保护模式边界,可能会导致非常意外的行为,包括挂起元件位置不起作用未传播的点击.为了警告人们这个潜在的问题,该功能在Java中的 INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS 和在.NET中的 IntroduceInstabilityByIgnoringProtectedModeSettings 中使用了一些吓人的大名字.我们确实以为告诉用户使用此设置会在他们的代码中引入潜在的缺陷,因此会阻止它的使用,但事实并非如此.

    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.

    如果您能够设置IE的保护模式设置,但仍在使用该功能,则可能会危及代码的稳定性.不要这样进行设置.并不难.

    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.

    您需要在此处设置 Protected Mode settings :

    Here is how you need to set the Protected Mode settings :

    • Here is another discussion on Selenium IEServerDriver not finding new windows for IE9 where the solution was Turning on Compatibility Mode

    这篇关于硒未检测到IE中的第二个窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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