1.6 selenium3+firefox環境搭建
1.6 selenium3+firefox環境搭建
有不少小夥伴在安裝selenium環境後啟動firefox報錯,因為現在selenium升級到3.0了,跟2.0的版本還有有一點區別的。
(備註:這裏不建議大家用selenium3,坑太多,如果selenium2實在用不了,那就看這篇吧)
安裝環境過程中主要會遇到三個坑:
1.‘geckodriver‘ executable needs to be in PATH
2.Expected browser binary location, but unable to find binary in default location
3.Unsupported Marionette protocol version 2, required 3
環境準備:
--python3.6
--selenium3.0
--firefox50
一、安裝python
1.安裝python這個簡單,下載版本後傻瓜式安裝就行了。
2.安裝好之後,看下這個目錄D:\python\Scripts,有沒pip.exe和easy_install.exe(一般都有,沒有的話得重新安裝一次了)
3.將D:\python和D:\python\Scripts,添加到環境變量path下
二、檢查pip工具
1.打開cmd,輸入:pip,出現如下圖界面,說明pip環境OK.
>>pip
三、安裝selenium3.0
1.cmd輸入:pip install selenium
>>pip install selenium
2.首次安裝要看到100%完成,中途失敗就重新多輸入幾次安裝。
四、檢查selenium環境
1.在cmd輸入如下指令檢查環境
>>python
>>from selenium import webdriver
>>driver=webdriver.Firefox()
>>driver.get("https://www.baidu.com")
2.能看到瀏覽器正常啟動,說明環境OK,如果遇到異常就繼續看下面解決方案。
五、遇到第一個坑:‘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 1, in <module>
File "D:\test\python3\lib\site-packages\selenium\webdriver\firefox\webdriver.py", 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>", line 1, in <module>
File "D:\test\python3\lib\site-packages\selenium\webdriver\firefox\webdriver.py", 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: Unsupported Marionette protocol version 2, required 3
2.這個錯誤原因是firefox版本過低了,最新的selenium3.0版本支持firefox47以上的版本,升級版本就可以了
總結:整個環境的配置是python3.6+selenium3.0+firefox47以上版本,當然python用2.7版本也是可以的。
1.6 selenium3+firefox環境搭建