1. 程式人生 > 實用技巧 >如何使用selenium呼叫谷歌瀏覽器

如何使用selenium呼叫谷歌瀏覽器

由於我安裝chrome時使用的自定義位置安裝的,所以自己在做這裡的時候遇到幾個問題
1.找不到驅動
selenium.common.exceptions.WebDriverException: Message: ‘chromedriver’ executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

2.找不到瀏覽器的二進位制檔案
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary

3.驅動程式載入失敗
selenium.common.exceptions.WebDriverException: Message: Service D:\Google\Chrome\Application\chrome.exe unexpectedly exited. Status code was: 0


解決方法:

1、去這裡(http://npm.taobao.org/mirrors/chromedriver/)下載對應版本的驅動程式, 並放到chrome.exe檔案的目錄中(第三種情況就是沒在同一目錄下導致的)
在這裡插入圖片描述

2、程式中載入驅動時加上驅動程式的路徑,如果使用預設值,則假定可執行檔案在環境變數中

driver = webdriver.Chrome(executable_path="(你的地址)Chrome\Application\chromedriver.exe")
在這裡插入圖片描述

這時再執行就可以自動控制瀏覽器了