Mac下使用selenium包時,"chromedriver' executable needs to be in PATH"

開發環境

操作系統 Mac OS 10.13.6
Python 3.7
Chrome 72.0
chromedriver 2.45

 使用selenium庫下webdriver模塊打開谷歌瀏覽器時報錯,代碼如下:

from selenium import webdriver
# 創建瀏覽器對象
driver = webdriver.Chrome()  # Chrome是谷歌瀏覽器(驅動:chromedriver),也可以更改其他瀏覽器注:更換其他瀏覽器時,需要安裝瀏覽器相對應的驅動
driver.get('https://www.baidu.com')  # 打開百度網頁
driver.close()  # 關閉瀏覽器

錯誤信息(1): 

selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

Chrome瀏覽器版本相對應的ChromeDriver版本,參考如下鏈接:

https://chromedriver.storage.googleapis.com/2.46/notes.txt

下載ChromeDriver驅動官網鏈接:
https://chromedriver.storage.googleapis.com/index.html

Geckodriver驅動下載地址:

https://github.com/mozilla/geckodriver/releases

Mac

將ChromeDriver驅動複製"/usr/local/bin"目錄下,ChromeDriver驅動文件路徑添加到系統文件內,同樣的方法配置GeckoDriver驅動

$ vim ~/.bash_profile # 進入環境變量文件
點 i 鍵進入編輯模式,進行編輯(必須是英文下的大寫字母或者小寫字母)
添加該文件路徑:export PATH=$PATH:/usr/local/bin/chromedriver
編輯完成後,點esc退出
輸入" :wq! ",這四個字符後,回車即可保存

運行上述代碼打開Google瀏覽器 

Windows

1.將geckodriver驅動複製到python根目錄下

2.我的電腦右擊找到屬性

3.進入高級系統設置

4.系統屬性頁面找到環境變量

5.進入環境變量 — 系統變量框中找到 "Path" — 編輯

6.將Geckodriver路徑拷貝到"Path"

注意:需要在路徑前面添加英文下的分號( ; ),路徑前面已經有分號了,則不需要再次添加

Path路徑添加完畢需要一直點擊 “ 確定按鈕 ”,這樣驅動就添加成功了,ChromeDriver驅動扔到python根目錄下,在path添加一個路徑就可以了,我實在編不下去了,您們隨便看看就行??

 

錯誤信息(2):

selenium.common.exceptions.WebDriverException: Message: Host header or origin header is specified and is not localhost——指定了主機報頭或源報頭,而不是本地主機。

解決方法:

下載瀏覽器所兼容的ChromeDriver驅動

Chrome瀏覽器版本相對應的ChromeDriver版本,參考鏈接:

https://chromedriver.storage.googleapis.com/2.46/notes.txt

錯誤信息(3): 


selenium.common.exceptions.WebDriverException:Message: Service chromedriver unexpectedly exited. Status code was: 126:chrome服務意外退出,狀態碼是:126

解決方案:

最新版本谷歌瀏覽器(ChromeGoogle版本—74 )沒有所兼容的chromedriver驅動,所以需要更換(低於74版本的)Google Chrome瀏覽器版本

查看Google Chrome版本信息  ⋮——>幫助——>關於Google Chrome

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章