1. 程式人生 > >【Python 解決錯誤】selenium.common.exception.WebDriverException

【Python 解決錯誤】selenium.common.exception.WebDriverException

平臺 地址 .org 版本 腳本 尋找 name body version

近來準備寫個腳本去搜索某端遊的官網交易平臺,用以尋找滿足條件的角色購入。

因為也不懂高端的爬蟲技術,就用selenium去戳。這裏采用的是chrome瀏覽器,鏈接網頁時報錯:

File "C:\Python37\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in __init__
self.service.start()
File "C:\Python37\lib\site-packages\selenium\webdriver\common\service.py", line 83, in start


os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: ‘chromedriver‘ executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

解決辦法:

安裝與瀏覽器版本匹配的webdriver

1、打開谷歌瀏覽器, 在地址欄輸入 chrome://version/ 查看版本信息,我的版本信息描述如下:

Google Chrome 72.0.3626.119 (正式版本) (32 位) (cohort: Stable)
修訂版本 9a65993e2cde1b5797ec98da4cd9abcea464cd7b-refs/branch-heads/3626@{#876}
操作系統 Windows

2、選擇合適版本的驅動下載, 如果沒有完全匹配的就選最相似匹配的版本下載。(比如我的版本沒有完全匹配的,我選了一個臨近的驅動,也可用)

下載地址:

http://chromedriver.storage.googleapis.com/index.html

技術分享圖片

3、解壓下載的驅動放到指定目錄,代碼調用時指定該目錄即可。

我把它放在了selenium下的chrome了,代碼演示如下

from selenium import webdriver

chrome_driver = r"C:\Python37\Lib\site-packages\selenium\webdriver\chrome\chromedriver.exe"
browser = webdriver.Chrome(executable_path=chrome_driver)

成功解決問題!

【Python 解決錯誤】selenium.common.exception.WebDriverException