題前話(Pre-words)
希望使用Selenium 2.0的人看到這篇文章能夠收藏此文,以后遇到該問(wèn)題,再也不用花費(fèi)多余的時(shí)間進(jìn)行research了!本文是對(duì)網(wǎng)上所有千奇百怪各種各樣的search結(jié)果所做的好總結(jié)。
問(wèn)題概述(Question)
IE11不支持Selenium 2.0,在什么情況下不支持呢?比如:獲取通過(guò)WebDriver獲取頁(yè)面上的元素時(shí),你經(jīng)常會(huì)捕獲到這樣的異常:
OpenQA.Selenium.NoSuchWindowException: Unable to find element on closed window
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse) in c:ProjectswebdriverdotnetsrcwebdriverRemoteRemoteWebDriver.cs:line 1139
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters) in c:ProjectswebdriverdotnetsrcwebdriverRemoteRemoteWebDriver.cs:line 923
at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(String mechanism, String value) in c:ProjectswebdriverdotnetsrcwebdriverRemoteRemoteWebDriver.cs:line 954
at SharePoint_Automation.mainForm.waitUntilPageLoaded(IWebDriver iw, String eleID) in C:UsersAdministratordocumentsvisual studio 2015ProjectsSharePoint AutomationSharePoint AutomationForm1.cs:line 116
上面這段異常的重點(diǎn)在于:
OpenQA.Selenium.NoSuchWindowException: Unable to find element on closed window
解決方案(Solution)
經(jīng)過(guò)不屑的努力,終讓我找到了這一問(wèn)題的解決方案如下(藍(lán)色字體為針對(duì)64bit計(jì)算機(jī)的終解決手段):
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. (這段告訴你需要修改注冊(cè)表。)
For 32-bit Windows installations, the key you must examine in the registry editor is HKEY_LOCAL_MACHINESOFTWAREMicrosoftInternet ExplorerMainFeatureControlFEATURE_BFCACHE. (32bit Windows看這里。)
For 64-bit Windows installations, the key is HKEY_LOCAL_MACHINESOFTWAREWow6432NodeMicrosoftInternet ExplorerMainFeatureControlFEATURE_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.(64bit Windows看這里。)
翻譯過(guò)來(lái)的意思即,修改你的注冊(cè)表(Run->regedit->Enter),路徑如下:
HKEY_LOCAL_MACHINESOFTWAREWow6432NodeMicrosoftInternet ExplorerMainFeatureControlFEATURE_BFCACHE
如果FeatureControl下沒(méi)有FEATURE_BFCACHE,以FEATURE_BFCACHE為名new一個(gè)key!并在其下創(chuàng)建一個(gè)DWORD,取名為:iexplore.exe,value為0。
修改完你的注冊(cè)表后,重啟你的計(jì)算機(jī)。之后以管理員權(quán)限運(yùn)行Visual Studio,打開(kāi)你的項(xiàng)目,運(yùn)行,你會(huì)發(fā)現(xiàn),問(wèn)題解決了。
Congratulations!這幾個(gè)小時(shí)沒(méi)有白費(fèi),終于讓我找到了解決方案!網(wǎng)上千奇百怪的說(shuō)法很多,但是沒(méi)有一個(gè)好使的!
希望看到這篇文章的人能夠收藏此文,以后遇到了這個(gè)問(wèn)題,再也不用花費(fèi)多余的時(shí)間進(jìn)行research了!
感謝以下兩篇文章:
Selenium - NoSuchWindowException in IE 11
InternetExplorerDriver
另:
若想正常使用WebDriver,請(qǐng)確保你的瀏覽器選項(xiàng)中security中各個(gè)zone的Protected Mode都是勾選著/非勾選著的,一定要統(tǒng)一才行!否則WebDriver將無(wú)法打開(kāi)瀏覽器,會(huì)有如下異常:
System.InvalidOperationException: Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones. Enable Protected Mode must be set to the same value (enabled or disabled) for all zones. (NoSuchDriver)