記錄踩過的坑-Selenium (Python)

目錄

 

'chromedriver' executable needs to be in PATH

selenium.common.exceptions.WebDriverException: Message: unknown Error: cannot find Chrome binary

selenium.common.exceptions.WebDriverException: Message: unknown error: Runtime.executionContextCreat

讀取js文件和執行js代碼


'chromedriver' executable needs to be in PATH

selenium.common.exceptions.WebDriverException: Message: unknown Error: cannot find Chrome binary

selenium.common.exceptions.WebDriverException: Message: unknown error: Runtime.executionContextCreat

 

上述問題要麼是Chromedriver版本和谷歌瀏覽器版本沒對上,要麼是配置有問題

1.下載Chromedriver,注意自己谷歌瀏覽器的版本和Chromedriver的版本必須對應。對應表請自行搜索。
2.將chromedriver.exe拷貝至谷歌瀏覽器目錄(如 C:\Program Files\Google\Chrome\Application)以及python根目錄。對於用anaconda的拷貝至envs裏你準備使用Selenium的環境的根目錄下。 
3.將谷歌瀏覽器環境變量添加到path。 如果這裏搞不定的在代碼中指定chrome.exe絕對路徑也可以。

option = webdriver.ChromeOptions()
option.binary_location=r'xxx\chrome.exe' #xxx請自行替換
driver = webdriver.Chrome()

 

設置js執行超時時間

driver.set_script_timeout(xxx)

 

讀取js文件和執行js代碼

首先這個js文件要存在

js = open("jquery-3.4.1.min.js", "r").read()

driver.execute_script(js)

 

執行js代碼,類似

比如form = ''' var fd = new window.FormData(); fd.append......'''

driver.execute_script(form)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章