selenium 安裝與 chromedriver安裝
阿新 • • 發佈:2020-12-02
安裝selenium
selenium可以直接可以用pip安裝。
pip install selenium
安裝chromedriver
下載
chromedriver的版本一定要與Chrome的版本一致,不然就不起作用。
有兩個下載地址:
1、http://chromedriver.storage.googleapis.com/index.html
2、https://npm.taobao.org/mirrors/chromedriver/
當然,你首先需要檢視你的Chrome版本,在瀏覽器中輸入chrome://version/
http://chromedriver.storage.googleapis.com/index.html?path=87.0.4280.20/
配置
解壓壓縮包,找到chromedriver.exe複製到chrome的安裝目錄(其實也可以隨便放一個資料夾)。複製chromedriver.exe檔案的路徑並加入到電腦的環境變數中去。具體的:
進入環境變數編輯介面,新增到使用者變數即可,雙擊PATH,將你的檔案位置(C:\Program Files\Google\Chrome\Application)新增到後面。
完成後在cmd下輸入chromedriver驗證是否安裝成功:
測試
未配置環境也可以,例如:
from selenium import webdriver import time def main(): chrome_driver= 'C:\Program Files\Google\Chrome\Application\chromedriver.exe' #chromedriver的檔案位置 b = webdriver.Chrome(executable_path = chrome_driver) b.get('https://www.baidu.com') time.sleep(5) b.quit() if __name__ == '__main__': main()
已配置環境變數時
from selenium import webdriver import timedef main(): b = webdriver.Chrome() b.get('https://www.baidu.com') time.sleep(5) b.quit() if __name__ == '__main__': main()