天天看點

Python3 中使用 selenium

文章目錄

    • Firefox浏覽器
      • 得下載下傳geckodriver
    • Chrome浏覽器
      • 得下載下傳chromedriver
    • 最後

在使用selenium之前,我們得做一些工作

Firefox浏覽器

得下載下傳geckodriver

官網傳送門

同時在下載下傳頁面詳細閱讀比對的firefox版本号與geckodriver版本等資訊

否則會報錯:

selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH. 
           

然後編輯程式:

from selenium import webdriver

browser = webdriver.Firefox(executable_path = 'D:\SoftWare\geckodriver-v0.21.0-win64\geckodriver.exe')
browser.get('http://www.baidu.com/')
           

如果出現錯誤:

selenium.common.exceptions.SessionNotCreatedException: Message: Unable to find a matching set of capabilities
           

考慮更新浏覽器版本

Chrome浏覽器

得下載下傳chromedriver

下載下傳傳送門

這個下載下傳得先檢視

LATEST_RELEASE

,然後下載下傳最新版(因為版本号最大的不一定是最新的)

否則會報錯:

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

編輯程式:

from selenium import webdriver

browser = webdriver.Chrome(executable_path = "D:\SoftWare\chromedriver_win32\chromedriver.exe")
browser.get('http://www.baidu.com/')
           

最後

  1. executable_path

    的值為你儲存的位置。
  2. 如果想要省略

    executable_path

    ,考慮将其值添加到環境變量中。或者将

    exe

    程式複制到python程式的根目錄下,比如說

    C:\Program Files\Python36\geckodriver.exe

    。然後重新打開你的開發環境。(複制到python的根目錄親測有效)
  3. 如果出現錯誤:
TypeError: get() missing 1 required positional argument: 'url'
           

問題可能出在

webdriver.Chrome()

的括号你忘寫了。