以下是我的环境:
- 操作系统:Windows 10 - 64位(家庭版)
- 浏览器:Firefox 47.0.1(32位)
- Python:2.7.10.12(64位)
- selenium:3.0.1
- Geckodriver:geckodriver-v0.11.1-win64.zip
Firefox安装在C:\Program Files (x86)\Mozilla Firefox
目录下。
geckodriver.exe文件位于C:\Python27\Scripts
目录中。
以下是我的Python代码:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://www.python.org")
这会导致以下错误:
Traceback (most recent call last):
File "examples1.py", line 5, in <module>
driver = webdriver.Firefox()
File "C:\Python27\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 152, in __init__
keep_alive=True)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 92, in __init__
self.start_session(desired_capabilities, browser_profile)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 179, in start_session
response = self.execute(Command.NEW_SESSION, capabilities)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, 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
我的问题是尽管Firefox已经安装在默认位置,但webdriver无法找到它并抛出了错误。
注意:当我明确指定Firefox二进制文件的位置如下所示时,它可以工作。
binary = FirefoxBinary(r'C:\Program Files (x86)\Mozilla Firefox\firefox.exe')
driver = webdriver.Firefox(firefox_binary=binary)