Selenium – IE 11中的NoSuchWindowException

我试图在IE11中使用selenium自动化网页。 我已将保护模式设置设置为相同级别,缩放级别为100%。 在运行测试时,它会打开网站,但之后会提供例外。 以下是使用的代码。

File file = new File("C:\\Users\\Desktop\\IEDriverServer.exe"); System.setProperty("webdriver.ie.driver", file.getAbsolutePath() ); DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer(); capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true); WebDriver driver = new InternetExplorerDriver(capabilities); driver.get("http://www.google.com"); 

和exception堆栈跟踪

 Started InternetExplorerDriver server (32-bit) 2.39.0.0 Listening on port 38122 Jul 11, 2014 1:50:02 PM org.apache.http.impl.client.DefaultRequestDirector tryExecute INFO: I/O exception (java.net.SocketException) caught when processing request: Software caused connection abort: recv failed Jul 11, 2014 1:50:02 PM org.apache.http.impl.client.DefaultRequestDirector tryExecute INFO: Retrying request Exception in thread "main" org.openqa.selenium.NoSuchWindowException: Unable to find element on closed window (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 18 milliseconds Build info: version: '2.39.0', revision: 'ff23eac', time: '2013-12-16 16:12:12' System info: host: 'Neeraj', ip: '10.136.180.161', os.name: 'Windows 7', s.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_60' Session ID: ab6edd65-8a66-41fa-be46-56fba7dbdfc9 Driver info: org.openqa.selenium.ie.InternetExplorerDriver Capabilities [{platform=WINDOWS, javascriptEnabled=true, elementScrollBehavior=0, ignoreZoomSetting=false, enablePersistentHover=true, ie.ensureCleanSession=false, browserName=internet explorer, enableElementCacheCleanup=true, unexpectedAlertBehaviour=dismiss, version=11, ie.usePerProcessProxy=false, cssSelectorsEnabled=true, ignoreProtectedModeSettings=true, requireWindowFocus=false, handlesAlerts=true, initialBrowserUrl=http://localhost:38122/, ie.forceCreateProcessApi=false, nativeEvents=true, browserAttachTimeout=0, ie.browserCommandLineSwitches=, takesScreenshot=true}] at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:193) at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554) at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:307) at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:348) at org.openqa.selenium.By$ById.findElement(By.java:220) at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:299) at Test1.main(Test1.java:27) 

有关如何解决此问题的任何建议。

首先,不要使用

 capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true); 

因为您已经设置了保护模式设置。 对于您看到的问题,应该是因为缺少的注册表设置作为在IE11中运行测试的先决条件而添加:

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

我使用IE 11 – 64位Windows机器 。 这一点对我有用。

仅对于IE 11,您需要在目标计算机上设置一个注册表项,以便驱动程序可以维护与其创建的Internet Explorer实例的连接。

对于32位Windows安装,您必须在注册表编辑器中检查的密钥是HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Internet Explorer \ Main \ FeatureControl \ FEATURE_BFCACHE。

对于64位Windows安装,密钥是HKEY_LOCAL_MACHINE \ SOFTWARE \ Wow6432Node \ Microsoft \ Internet Explorer \ Main \ FeatureControl \ FEATURE_BFCACHE。

请注意,FEATURE_BFCACHE子项可能存在也可能不存在,如果不存在,则应创建该子项。

要点:在此项中,创建名为iexplore.exe的值为0的DWORD值。

@David Kemp建议的解决方案不适用于11位窗口10 – 64位 。 我已经按照IE 11中提到的步骤添加了注册表项HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet explorer\Main\FeatureControl\FEATURE_BFCACHE ,仅在https://github.com/SeleniumHQ/selenium/wiki/InternetExplorerDriver#required-之后配置但在打开https证书页面(url https:// my-page:9443 )后,它无法执行driver.navigate().to("javascript:document.getElementById('overridelink').click()"); 抛出NoSuchWindowException

然而,同样适用于11位Windows 7-64位且能够执行脚本。

让11为win 10工作的工作是将initialBrowserUrlfunction设置为https:// my-page:9443,如下所示

 capabilities.setCapability("initialBrowserUrl", "https://my-page:9443"); 

但我仍然困惑为什么ie11 / windows 10它有所不同?

我发现,如果启动的浏览器保持焦点,你将获得该exception。 一旦启动webdriver,打开任何其他窗口,例如,您可以在脚本启动IE Driver后立即打开eclipse。 脚本执行开始,然后您可以打开IE驱动程序..通过脚本处理它,您添加以下代码:

  public WebDriver driver, driver1; System.setProperty("webdriver.ie.driver", System.getProperty( "webdriver.ie.driver", "./BrowserDrivers/IEDriverServer.exe")); driver = new InternetExplorerDriver(cap); this.driver.manage().deleteAllCookies(); this.driver.manage().timeouts().implicitlyWait(WaitTimeConstants.WAIT_TIME_LONG, TimeUnit.SECONDS); this.driver.get("yourApplication.com"); this.driver.manage().window().maximize(); public WebDriver driver, driver1; System.setProperty("webdriver.ie.driver", System.getProperty( "webdriver.ie.driver", "./BrowserDrivers/IEDriverServer.exe")); driver1 = new InternetExplorerDriver(cap); this.driver1.manage().deleteAllCookies(); this.driver1.get("http://www.google.com"); this.driver1.manage().window().maximize(); 

在“Internet选项”中为“可信站点”列表添加了AUT域。 解决了这个问题。

下面的解决方案也适用于从当前页面导航到下一页的某些操作/事件和selenium驱动程序无法识别窗口: –

对于64位Windows安装,关键是:

  HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet explorer\Main\FeatureControl\FEATURE_BFCACHE 

在其中创建一个名为iexplore.exe的值为0的DWORD值。

将http:// localhost /添加到IE11中的可信站点。 在尝试了其他一切没有结果之后,这对我有用。

IE选项 – >安全选项卡 – >取消选中“启用保护模式”对我有用。