1. 程式人生 > 程式設計 >Python3+selenium配置常見報錯解決方案

Python3+selenium配置常見報錯解決方案

第一個坑:'geckodriver' executable needs to be in PATH

1.如果啟動瀏覽器過程中報如下錯誤

Traceback (most recent call last):
File "<stdin>",line 1,in <module>
File "D:\test\python3\lib\site-packages\selenium\webdriver\firefox\webdriver.py",line 145,in __init__
self.service.start()
File "D:\test\python3\lib\site-packages\selenium\webdriver\common\service.py",line 81,in start

os.path.basename(self.path),self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.

2.這個是因為最新的selenium3.0啟動firefox需要geckodriver.exe這個驅動檔案。

3.下載之後,配置到環境變數path下(可以直接放python根目錄)

第二坑:Expected browser binary location,but unable to find binary in default location


1.如果啟動瀏覽器過程中報如下錯誤:

Traceback (most recent call last):
File "<stdin>",line 155,in __init__
keep_alive=True)
File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py",line 92,in __init__
self.start_session(desired_capabilities,browser_profile)

File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py",line 179,in start_session

response = self.execute(Command.NEW_SESSION,capabilities)
File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py",line 238,in execute
self.error_handler.check_response(response)
File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\errorhandler.py",line 193,in check_response
raise exception_class(message,screen,stacktrace)
selenium.common.exceptions.WebDriverException: Message: Expected browser binary location,but unable to find binary in default location,
no 'moz:firefoxOptions.binary' capability provided,and no binary flag set on the command line.

2.這個是因為firefox.exe這個檔案也需要配置到環境變數path下。

3.這個路徑就是安裝完firefox後,找到firefox.exe這個檔案的地址,加到path下。

第三坑:Unsupported Marionette protocol version 2,required 3

1.如果啟動瀏覽器過程中出現如下錯誤

Traceback (most recent call last):
File "<stdin>",in <module>

File "D:\test\python3\lib\site-packages\selenium\webdriver\firefox\webdriver.py",browser_profile)
File "D:\test\python3\lib\site-packages\selenium\webdriver\remote\webdriver.py",in check_response

raise exception_class(message,stacktrace)
selenium.common.exceptions.WebDriverException: Message: Unsupported Marionette protocol version 2,required 3

2.這個錯誤原因是firefox版本過低了,最新的selenium3.0版本支援firefox47以上的版本,升級版本就可以了

第四坑:WebDriverException: Message: newSession

1.Traceback (most recent call last):

File “D:\test\python3\lib\site-packages\selenium\webdriver\firefox\\webdriver.py”,line 170,in init
keep_alive=True)
File “D:\test\python3\lib\site-packages\selenium\webdriver\firefox\\webdriver.py”,line 156,in init
self.start_session(capabilities,browser_profile)
File “D:\test\python3\lib\site-packages\selenium\webdriver\firefox\\webdriver.py”,line 245,parameters)
File “D:\test\python3\lib\site-packages\selenium\webdriver\firefox\\webdriver.py”,line 314,in execute
self.error_handler.check_response(response)
File “D:\test\python3\lib\site-packages\selenium\webdriver\firefox\\errorhandler.py”,line 242,stacktrace)
selenium.common.exceptions.WebDriverException: Message: newSession

2.下載最新的geckodriver.exe 然後把它放到python的安裝目錄下

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。