一、安裝selenium
pip install selenium (有的可能是pip3 install selenium)
二、安裝chrome_webdriver
注:chromedriver的安裝一定要與Chrome的版本一致,不然就不起作用,還會報錯哦
這里有兩個下載地址:
(1) http://chromedriver.storage.googleapis.com/index.html
(2) https://npm.taobao.org/mirrors/chromedriver/
1.查看本地chrome的版本:
瀏覽器輸入chrome://version/

這個就是當前瀏覽器的版本。
下載對對應版本的webdriver即可
例如,我的版本是79.0.3945.130,我下載了79.0.3945.36版本的webdriver


這里要另外說一下,window的driver貌似沒有64位的,下了32位的也可以用。
2.下載完成后解壓,復制文件“chromedriver.exe”到chrome的安裝路徑下

3.配置環境變量,具體如下:
進入環境變量編輯界面,添加到用戶變量即可,雙擊PATH,將你的文件位置(我的是C:\Program Files (x86)\Google\Chrome\Application\)添加到后面。

完成后在cmd下輸入chromedriver驗證是否安裝成功:

3.測試是否可用:
打開pycharm,運行如下代碼:
from selenium import webdriver import time def main(): b=webdriver.Chrome() b.get('https://www.baidu.com') time.sleep(5) b.quit() if __name__ == '__main__': main()
如果正常打開了瀏覽器,打開網頁后退出,就是安裝成功的,
如果報了'chromedriver' executable needs to be in Path的錯誤,說明chromedriver下載的版本可能不對或者環境變量沒配對。
4.不配環境變量其實也沒關系,只是需要在代碼中定義webdriver的絕對路徑:
如:
import time from selenium import webdriver driver_path=r'C:\Users\94596\Downloads\chromedriver_win32\chromedriver.exe' #把瀏覽器驅動器放在任意位置都可以 driver=webdriver.Chrome(executable_path=driver_path) #定義driver時,指定driver路徑即可。 driver.get('http:www.baidu.com') time.sleep(5) driver.quit()
