IE11不支持Selenium 2.0的解決方法


題前話(Pre-words)

希望使用Selenium 2.0的人看到這篇文章能夠收藏此文,以后遇到該問題,再也不用花費多余的時間進行research了!本文就是對網上所有千奇百怪各種各樣的search結果所做的最好總結。

問題概述(Question)

IE11不支持Selenium 2.0,在什么情況下不支持呢?比如:獲取通過WebDriver獲取頁面上的元素時,你經常會捕獲到這樣的異常:

OpenQA.Selenium.NoSuchWindowException: Unable to find element on closed window
   at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse) in c:\Projects\webdriver\dotnet\src\webdriver\Remote\RemoteWebDriver.cs:line 1139
   at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters) in c:\Projects\webdriver\dotnet\src\webdriver\Remote\RemoteWebDriver.cs:line 923
   at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(String mechanism, String value) in c:\Projects\webdriver\dotnet\src\webdriver\Remote\RemoteWebDriver.cs:line 954
   at SharePoint_Automation.mainForm.waitUntilPageLoaded(IWebDriver iw, String eleID) in C:\Users\Administrator\documents\visual studio 2015\Projects\SharePoint Automation\SharePoint Automation\Form1.cs:line 116

上面這段異常的重點在於:

OpenQA.Selenium.NoSuchWindowException: Unable to find element on closed window

解決方案(Solution)

經過不屑的努力,最終讓我找到了這一問題的解決方案如下(藍色字體為針對64bit計算機的最終解決手段):

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. (32bit Windows看這里。)

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.(64bit Windows看這里。)

翻譯過來的意思即,修改你的注冊表(Run->regedit->Enter),路徑如下:

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

如果FeatureControl下沒有FEATURE_BFCACHE,就以FEATURE_BFCACHE為名new一個key!並在其下創建一個DWORD,取名為:iexplore.exe,value為0。

修改完你的注冊表后,重啟你的計算機。之后以管理員權限運行Visual Studio,打開你的項目,運行,你會發現,問題解決了。

Congratulations!這幾個小時沒有白費,終於讓我找到了解決方案!網上千奇百怪的說法很多,但是沒有一個好使的!

希望看到這篇文章的人能夠收藏此文,以后遇到了這個問題,再也不用花費多余的時間進行research了!

感謝以下兩篇文章:

Selenium - NoSuchWindowException in IE 11

InternetExplorerDriver

另:

若想正常使用WebDriver,請確保你的瀏覽器選項中security中各個zone的Protected Mode都是勾選着/非勾選着的,一定要統一才行!否則WebDriver將無法打開瀏覽器,會有如下異常:

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)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM