gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:11:21 24 4
gpt4 key购买 nike

我的应用程序在单击按钮时会打开一个新窗口,我需要在该窗口中执行一些操作。但是selenium webdriver的response getWindowHandles()方法里面只有一个window 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://<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]" 

但是如果我删除 sleep 。它将返回两个窗口 ID:

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

我无法取消 sleep ,因为这只是一个示例程序,在实际应用中,两者之间会有一些延迟。请让我知道您的想法。此问题仅适用于 IE11。

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

enter image description here

最佳答案

在处理 InternetExplorer 时,您必须注意几件事如下:

正如您提到的 There is a known issue with selenium记录在 GitHub ,这些不是问题本身,而是 Required Configuration 的组合集在处理 InternetExplorer。不注意这些设置InternetExplorer可能不会按预期行事。以下项目对于证明 InternetExplorer v11 的正确行为至关重要:

  • Enhanced Protected Mode 必须为 IE 10 及更高版本禁用。此选项位于 AdvancedInternet Options 的选项卡对话。
  • 浏览器 Zoom Level 必须设置为 100%,以便可以将 native 鼠标事件设置为正确的坐标。
  • 你必须设置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 : 使用 native 事件的优点是它不依赖于 JavaScript 沙箱,并确保在浏览器中正确传播 JavaScript 事件。但是,当前在 IE 浏览器窗口没有焦点以及尝试将鼠标悬停在元素上时鼠标事件存在一些问题。

  • Browser Focus : 如果窗口没有焦点,IE 本身似乎没有完全遵守我们发送给 IE 浏览器窗口的 Windows 消息(WM_MOUSEDOWN 和 WM_MOUSEUP)。

  • 您可以在 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);
  • 根据 Best Programming做法<强>Thread.sleep(1000); 是一个巨大的,因为它会降低 Test Performance

  • 现在,如您所知,Browser Clients滞后于 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 在 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.

这里是你需要如何设置 Protected Mode settings :

Protected Mode IE

关于java - Selenium 没有检测到 IE 中的第二个窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48162835/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com