问题
I'm having trouble running geckodriver with Python 3. I recently switched to Python 3 with an application that I've been working on, and have updated Firefox(53.0), Selenium(3.4.3), and geckodriver(0.17.1). I'm also using OSX and used pip to install all of my packages.
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
# Set Firefox Settings
# binary = FirefoxBinary('Users/username/Applications/Firefox.app/Contents/MacOS/firefox')
# binary = FirefoxBinary('/Applications/Firefox.app/Contents/MacOS/firefox')
# binary = FirefoxBinary('/Applications/Firefox.app/Contents/MacOS/firefox-bin')
path = '/usr/local/bin/geckodriver'
profile = webdriver.FirefoxProfile()
browser = webdriver.Firefox(executable_path=path,
firefox_profile=profile,
firefox_binary=binary)
browser.get("http://google.com")
The web browser will start, but then I will receive the following error:
Traceback (most recent call last):
File "/Users/jphubert/Desktop/AbstractionProject/py/browsertest.py", line 11, in <module>
firefox_profile=profile),
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/firefox/webdriver.py", line 155, in __init__
keep_alive=True)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 92, in __init__
self.start_session(desired_capabilities, browser_profile)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 183, in start_session
self.capabilities = response['value']
KeyError: 'value'
I have uninstalled and reinstalled Firefox and geckodriver, taking @Viragos advice to ensure I installed the MacOS version. @Debanjan set me on the right track to try and set the Firefox binary and include it in the webdriver profile but I'm still getting the same error.
I've tried taking the Firefox binary from GitHub and putting it in a .py file as well, and have made attempts to remove the profile and executable path, but the same problem persists. My binary files are in the correct location and it works if I follow the path myself and click through their .exe files, but I can't make my script run anymore.
I've been running Selenium without any issues on Python 2.7, and only since yesterday, upgrading gecko and Python, have I been having problems.
Thank you!
回答1:
Updating to geckodriver 0.17.0 fixed the issue for me
Firefox 53.0.3
Selenium 3.4.3
Python 3.6
binary = FirefoxBinary('C:\\Program Files (x86)\\Mozilla Firefox\\Firefox.exe')
driver = webdriver.Firefox(firefox_binary=binary)
driver.get(url)
回答2:
Here is the Answer to your Question:
Using Selenium 3.4.x, Python 3.6.1 along with geckodriver v0.16.1 & Mozilla Firefox 53.0, you can configure the following parameters:
firefox_binary
: To provide the absolute path of the exact Firefox Binary you intend to use.firefox_profile
: To specify if you want to start with a new/existing Firefox Profile.executable_path
: To specify absolute path of the geckodriver.exe.
It is to be noted that the current Selenium-Python binding is unstable with geckodriver and looks to be Architecture specific. You can find the github discussion and merge here. So you may additionally need to pass the absolute path of the firefox binary as
firefox_binary
argument while initializing the webdriver
The following code block will open a Firefox Browser as per the configuration we set through the previous mentioned parameters:
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
binary = FirefoxBinary('C:\\Program Files\\Mozilla Firefox\\firefox.exe')
profile = webdriver.FirefoxProfile()
path = "C:\\Utility\\BrowserDrivers\\geckodriver.exe"
browser = webdriver.Firefox(executable_path=path, firefox_profile=profile,firefox_binary=binary)
browser.get("http://google.com")
Let me know if this Answers your Question.
回答3:
I also met this problem when I tried to run the code at home with geckodriver in Pycharm. I added the geckodriver folder to System Environment Path. And then I updated Pycharm as well as the Selenium version and then it just fixed the problem. I could run the code from CMD before updating my Selenium version. After updating the Selenium version in Pycharm. It's working again. It's weird.
I aslo tried to add the geckodriver path before but it didn't fix the problem.
driverPath = 'C:\\Users\\xxx\\OneDrive\\xxx\\geckodriver.exe'
driver = webdriver.Firefox(firefox_profile=profile, executable_path=driverPath)
来源:https://stackoverflow.com/questions/44447042/python-3-selenium-keyerror-value-issue-wont-initialize-geckodriver-for-firef