1. 程式人生 > >【Python】selenium調用IE11瀏覽器,報錯“找不到元素”NoSuchWindowException: Message:Unable to find element on closed window

【Python】selenium調用IE11瀏覽器,報錯“找不到元素”NoSuchWindowException: Message:Unable to find element on closed window

conn ont csdn creates logs 註冊 target get 意思

當編寫自動化腳本,定位瀏覽器元素時,報如下錯誤:

代碼:

>>> # coding=utf-8
>>> from selenium import webdriver
>>> driver = webdriver.Ie()
>>> driver.get("www.baidu.com")
>>> driver.find_element_by_id("kw").send_keys("Selenium2")

報錯:
技術分享

解決方法:

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.exevalue0

技術分享

修改完你的註冊表後,重啟你的計算機。

【參照】:http://blog.csdn.net/angel_xiaa/article/details/52212340

但是發現我再定位元素時候還是報錯,我就懷疑是不是Selenium版本問題,因為我安裝的是Selenium3.X的版本,然後我運行cmd把3.X卸載重新安裝2.X版本就好了。

卸載命令:pip uninstall selenium //卸載當前安裝的包

安裝命令:pip install selenium==2.48.0 //指定版本號安裝

查看命令:pip show selenium //查看當前包的版本信息

【Python】selenium調用IE11瀏覽器,報錯“找不到元素”NoSuchWindowException: Message:Unable to find element on closed window