[Python] Selenium with chromedriver send_keys exception

  • 640
  • 0
  • 2018-03-25

最近發現原先寫好的 Python 自動化程式「send_keys」部份出現異常,會吐出「selenium.common.exceptions.WebDriverException: Message: unknown error: call function result missing 'value'」的錯誤訊息,經查發現是因為 Chrome 自動更新後導致與 chromedriver 版本不匹配所衍生的問題,這邊簡單記錄一下解決方式。

簡單的說,Chrome v0.65 必須搭配 chromedriver v2.36 以上的版本。然而,使用 pip 更新 chromedriver 後,發現無論怎麼更新都只有 v2.24.1,改用 chromedriver-binary 解決此問題。

期間我也一併把 pip、selenium、chromedriver 等套件都更新了,所以不是很確定是否要更新這些東西,如果還是不行可以都一併更新了試試。
  1. 查看 chromedriver 版本,確認版本是否真的過舊:
    (如果以下指令無法使用,請檢查一下 pip.exe 的所在目錄是否位於環境變數 PATH 中,或是直接到 pip.exe 所在目錄下執行)
    pip show chromedriver
  2. 安裝 chromedriver-binary
    pip install chromedriver-binary
  3. 修改 Python 程式碼
    from selenium import webdriver

    新增一行成為:

    from selenium import webdriver
    import chromedriver_binary  # Adds chromedriver binary to path


    原來的:

    driver = webdriver.Chrome(r'C:\Users\XXXX\AppData\Local\Programs\Python\Python35\download\chromedriver.exe')

    改為:

    driver = webdriver.Chrome()

     

再次執行,搞定! 

 

附註:

  • 更新 pip 方式:
    python -m pip install --upgrade pip
  • 更新套件方式:
    pip install -U chromedriver