selenium 安裝與 chromedriver安裝


安裝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/

例如我的版本是72.0.3626,所以下載

 配置

解壓壓縮包,找到chromedriver.exe復制到chrome的安裝目錄(其實也可以隨便放一個文件夾)。復制chromedriver.exe文件的路徑並加入到電腦的環境變量中去。具體的:

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

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

 測試

未配置環境也可以,例如:

from selenium import webdriver
import time

def main():
    chrome_driver = 'C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe'  #chromedriver的文件位置
    b = webdriver.Chrome(executable_path = chrome_driver)
    b.get('https://www.google.com')
    time.sleep(5)
    b.quit()

if __name__ == '__main__':
    main()

已配置環境變量時

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的版本不對(不要問我怎么知道的)。



參考鏈接:

1、https://blog.csdn.net/qq_41429288/article/details/80472064

2、https://www.cnblogs.com/LeslieForever/p/8317158.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM