I am trying to install Selenium WebDriver with Python on my Mac. I used this command:
sudo easy_install selenium
After that, I tried the following simple test:
python
from selenium import webdriver
driver = webdriver.Firefox()
And I got the following error. What am I doing wrong?
Traceback (most recent call last): File "", line 1, in File "/Library/Python/2.7/site-packages/selenium-3.0.0.b3-py2.7.egg/selenium/webdriver/firefox/webdriver.py", line 68, in init self.service.start() File "/Library/Python/2.7/site-packages/selenium-3.0.0.b3-py2.7.egg/selenium/webdriver/common/service.py", line 71, in start os.path.basename(self.path), self.start_error_message) selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
If you call a selenium driver without any arguments, the path to the webdriver executable must be in the system PATH environment variables.
Alternatively, you can specify the path explicitly as such:
driver = webdriver.Firefox("path/to/the/FireFoxExecutable")
The error is telling you that it can't find geckodriver
. geckodriver is an additional component that you must install to control Firefox. It's not included with the selenium package, so it must be installed separately.
The following shell script will download the latest geckodriver from Mozilla's repo and place it in usr/local/bin
, so it can be found on your PATH:
#!/bin/sh
url=$(curl -s "https://api.github.com/repos/mozilla/geckodriver/releases/latest" | python -c "import sys, json; r = json.load(sys.stdin); print [a for a in r['assets'] if 'linux64' in a['name']][0]['browser_download_url'];")
curl -L -o geckodriver.tar.gz $url
tar -xzf geckodriver.tar.gz
chmod +x geckodriver
sudo mv geckodriver /usr/local/bin
(run this script after you install selenium via pip
or easy_install
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.