NoSuchWindowException - 尽管遵循其他答案中的配置建议,但无法在 IE 上的 Selenium 中找到已关闭 window 上的元素

NoSuchWindowException - Unable to find element on closed window in Selenium on IE in spite of following the configuration advice in other answers

这与 Selenium 上的 有关(那个问题是关于 Firefox 特定的问题,这个问题是关于 IE 特定的问题)。

基本上,当我运行下面的代码

ieDriver.Navigate().GoToUrl("http://localhost:51282");
IWebElement linkToAboutPage = ieDriver.FindElement(By.Id("test"));
linkToAboutPage.Click();

模拟点击 link,它成功导航到页面,但当它尝试检索实际元素时,我得到以下异常:

An exception of type 'OpenQA.Selenium.NoSuchWindowException' occurred in WebDriver.dll but was not handled in user code

Additional information: Unable to find element on closed window

接受的 this question 答案建议 IE 安全设​​置中的 "Enable Protected Mode" 应该全部选中或全部取消选中。事实上,当我查看这些设置时,Int运行et 未选择 "Enable Protected Mode",但其他设置未选择:

不幸的是,如屏幕截图所示,这由我公司的 IT 部门管理,我不确定我能否成功说服他们让我更改设置。我也无法按照其他一些答案建议的方式编辑我的注册表(可能是由于缺乏管理权限)。

我见过的其他一些解决方案包括将 IntroduceInstabilityByIgnoringProtectedModeSettings 设置为 true、提供 InitialBrowserUrl 或将 EnsureCleanSession 设置为 true。如下所示,我目前正在做所有这些事情:

var ieOptions = new InternetExplorerOptions()
{
    InitialBrowserUrl = "http://www.google.com",
    IntroduceInstabilityByIgnoringProtectedModeSettings = true,
    IgnoreZoomLevel = true,
    EnableNativeEvents = true,
    EnsureCleanSession = true
};

ieDriver = new InternetExplorerDriver(ieOptions);
ieDriver.Manage().Timeouts().ImplicitWait = new TimeSpan(0, 0, 10);

但是,我仍然遇到完全相同的问题。

还有什么我可以尝试的,不涉及我为政策例外而窃听公司 IT 吗?

也许重要的是,当我在 localhost 上 运行 时,这 发生(这是一个问题,因为这是我打算做的大部分事情我的测试)。

我发现将 InitialBrowserUrl 功能设置为您要导航到的起始 URL,与 IntroduceInstabilityByIgnoringProtectedModeSettings = true 配对,对我有用。

var ieOptions = new InternetExplorerOptions()
{
    InitialBrowserUrl = <your-starting-url>
    IntroduceInstabilityByIgnoringProtectedModeSettings = true,
    ...
};

不幸的是,我不知道为什么会这样,所以这 "fix" 可能只是轶事...


您可以尝试以下其他一些解决方案(来自官方参考资料):

Required Configuration

  • The IEDriverServer exectuable must be downloaded and placed in your PATH.
  • On IE 7 or higher on Windows Vista or Windows 7, you must set the Protected Mode settings for each zone to be the same value. The value can be on or off, as long as it is the same for every zone. To set the Protected Mode settings, choose "Internet Options..." from the Tools menu, and click on the Security tab. For each zone, there will be a check box at the bottom of the tab labeled "Enable Protected Mode".
  • Additionally, "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.
  • For IE 11 only, 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 must examine in the registry editor 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. Please note that the FEATURE_BFCACHE subkey may or may not be present, and should be created if it is not present. Important: Inside this key, create a DWORD value named iexplore.exe with the value of 0.

参考:

https://github.com/SeleniumHQ/selenium/wiki/InternetExplorerDriver#required-configuration