Java Selenium - IE 11 中的 NoSuchWindowException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24746777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selenium - NoSuchWindowException in IE 11
提问by Wanderer
I am trying to automate a webpage using selenium in IE11. I have set the protected mode settings to same level and zoom level is 100%. While running the test it opens the website however gives the exception just after. Below is the code used.
我正在尝试在 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");
And the exception stacktrace
和异常堆栈跟踪
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)
Any suggestions on how to resolve this.
有关如何解决此问题的任何建议。
采纳答案by Sitam Jana
First of all, don't use
首先,不要使用
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
as you have already set Protected mode settings. For the issue you are seeing, it should be because of the missing registry settings that is added as a prerequisite for running tests in IE11:
因为您已经设置了保护模式设置。对于您看到的问题,应该是因为缺少注册表设置,这是在 IE11 中运行测试的先决条件:
https://github.com/SeleniumHQ/selenium/wiki/InternetExplorerDriver#required-configuration
https://github.com/SeleniumHQ/selenium/wiki/InternetExplorerDriver#required-configuration
回答by Solomon Raja
I am using IE 11 - 64 bit windows machine. This point worked for me.
我正在使用IE 11-64 位 Windows 机器。这一点对我有用。
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.
仅对于 IE 11,您需要在目标计算机上设置一个注册表项,以便驱动程序可以保持与它创建的 Internet Explorer 实例的连接。
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.
对于 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。
Please note that the FEATURE_BFCACHE subkey may or may not be present, and should be created if it is not present.
请注意 FEATURE_BFCACHE 子项可能存在也可能不存在,如果不存在则应创建。
Important:Inside this key, create a DWORD
value named iexplore.exe
with the value of 0.
重要提示:在此键内,创建一个DWORD
名为iexplore.exe
0 的值。
回答by Pallavi
I found that, if the launched browser is kept on focus, you will get that exception. As soon as you launch the webdriver, open any other window, for example, you can open eclipse as soon as the script launches IE Driver. Script execution starts, and then you can open you IE Driver.. to handle it through scripts, you add the below code:
我发现,如果启动的浏览器保持在焦点上,你会得到那个异常。启动 webdriver 后,立即打开任何其他窗口,例如,脚本启动 IE 驱动程序后即可打开 eclipse。脚本执行开始,然后你可以打开你的IE Driver..通过脚本来处理它,你添加以下代码:
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();
回答by meenal
Below solution also works if from current page you navigate on next page on some action/event and selenium driver doesnt recognise window :-
如果您从当前页面导航到某些操作/事件的下一页并且 selenium 驱动程序无法识别窗口,则以下解决方案也有效:-
For 64-bit Windows installations, the key is:
对于 64 位 Windows 安装,关键是:
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet explorer\Main\FeatureControl\FEATURE_BFCACHE
Inside this create a DWORD value named iexplore.exe with the value of 0.
在其中创建一个名为 iexplore.exe 的 DWORD 值,值为 0。
回答by Som
The solution suggested by @David Kemp is not working for the ie 11 of windows 10 - 64 bit. I have added the registry key HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet explorer\Main\FeatureControl\FEATURE_BFCACHE
according to the steps mentioned For IE 11 only following https://github.com/SeleniumHQ/selenium/wiki/InternetExplorerDriver#required-configurationbut after opening the https certificate page (url https://my-page:9443) it is unable to execute driver.navigate().to("javascript:document.getElementById('overridelink').click()");
throwing NoSuchWindowException
@David Kemp 建议的解决方案不适用于 ie 11 of windows10-64 bit。我已HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet explorer\Main\FeatureControl\FEATURE_BFCACHE
根据仅在https://github.com/SeleniumHQ/selenium/wiki/InternetExplorerDriver#required-configuration之后针对 IE 11 提到的步骤添加了注册表项 ,但在打开 https 证书页面(网址https://my- page:9443) 无法执行 driver.navigate().to("javascript:document.getElementById('overridelink').click()");
抛出NoSuchWindowException
However same works fine for ie 11 of windows 7-64 bitand able to execute the scripts .
然而,同样适用于 ie 11 的 7-64 位 Windows并且能够执行脚本。
The work around to make ie 11 work for win 10 is by setting initialBrowserUrlcapabilities to https://my-page:9443like below
使 ie 11 适用于 win 10 的解决方法是将initialBrowserUrl功能设置为 https://my-page:9443,如下所示
capabilities.setCapability("initialBrowserUrl", "https://my-page:9443");
but I am still confused why for ie11 / windows 10 it's different ?
但我仍然很困惑,为什么 ie11/windows 10 不同?
回答by IchimGA
Add http://localhost/to your trusted sites in IE11. This worked for me,after trying everything else without results.
将http://localhost/添加到您在 IE11 中的受信任站点。在尝试了其他一切没有结果之后,这对我有用。
回答by Jeevan Adiga
Added domain of AUT to list of "Trusted Sites" for i.e. in "Internet Options". Solved the problem.
在“Internet 选项”中,将 AUT 域添加到“可信站点”列表中。解决了问题。
回答by Ponns
IE Options --> Security Tab -> Uncheck "Enable Protected Mode" worked for me.
IE 选项 --> 安全选项卡 -> 取消选中“启用保护模式”对我有用。
回答by Venkatesh Pothula
Ignore above all... i have tried below line in my desired capabilities for IE driver then its worked .. :)
最重要的是忽略......我已经在我想要的 IE 驱动程序功能中尝试了以下行,然后它起作用了.. :)
ieCapabilities.setCapability(InternetExplorerDriver.INITIAL_BROWSER_URL,false);
回答by Samarth
You can follow a few options from below:
您可以遵循以下几个选项:
- Use relative paths in the project, later you can choose to get its absolute path.
- Setting the Protected mode setting for all, to either enabled or disabled.
- Try understanding what really this exception mean "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"
- 在项目中使用相对路径,后面可以选择获取其绝对路径。
- 将所有保护模式设置为启用或禁用。
- 尝试了解此异常的真正含义“org.openqa.selenium.NoSuchWindowException:无法在关闭的窗口上找到元素(警告:服务器未提供任何堆栈跟踪信息)命令持续时间或超时:18 毫秒”
This states that whatever being done by your code (the web driver) is not able to identify the window of IE.
这表明您的代码(网络驱动程序)所做的任何事情都无法识别 IE 的窗口。
The possible causes can be: a. Multiple driver instances are already running which were instantiated by older runs and could not have been terminated by quite(). b. The sometimes when you use close() on a driver it will not terminate the IE Driver, so use quite() after closing inside a finally block. Yes, add some Exception handling to avoid the above issue.
可能的原因可能是:多个驱动程序实例已经在运行,这些实例由较旧的运行实例化,并且无法通过quit() 终止。湾 有时当您在驱动程序上使用 close() 时,它不会终止 IE 驱动程序,因此在 finally 块内关闭后使用 compare() 。是的,添加一些异常处理以避免上述问题。
Exception "org.openqa.selenium.NoSuchWindowException" also suggests that web driver instance is not able to get the handle of IE's window tab. I faced this issue when accidentally, the browser was closed and the code was still trying to locate and do some action on a web element. In this case, the driver throws this exception to tell that there is no IE browser opened at all.
Having a check on the opening of browser can also help, I usually keep some piece of code that gives logs of used Driver object and the port where it is trying to connect and to which browser to.
异常 "org.openqa.selenium.NoSuchWindowException" 还表明 Web 驱动程序实例无法获取 IE 窗口选项卡的句柄。我偶然遇到了这个问题,浏览器已关闭,代码仍在尝试定位并在 Web 元素上执行某些操作。在这种情况下,驱动程序会抛出此异常以表明根本没有打开 IE 浏览器。
检查浏览器的打开情况也有帮助,我通常会保留一些代码,这些代码提供使用过的 Driver 对象的日志以及它尝试连接的端口以及连接到哪个浏览器的端口。
For me, point 4 & 5 worked. but you might wanna just change the web driver's service i.e. browser. IE is just too slow sometimes to honour the selenium API calls :(
对我来说,第 4 点和第 5 点有效。但您可能只想更改网络驱动程序的服务,即浏览器。IE 有时太慢而无法满足 selenium API 调用:(