简体   繁体   English

无法使用Python安装Selenium WebDriver

[英]Can't install Selenium WebDriver with Python

I am trying to install Selenium WebDriver with Python on my Mac. 我正在尝试在我的Mac上安装带有Python的Selenium WebDriver。 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. 回溯(最近一次调用最后一次):文件“”,第1行,在文件“/Library/Python/2.7/site-packages/selenium-3.0.0.b3-py2.7.egg/selenium/webdriver/firefox/webdriver 。self“,第68行,在init self.service.start()文件”/Library/Python/2.7/site-packages/selenium-3.0.0.b3-py2.7.egg/selenium/webdriver/common/service .py“,第71行,在启动时os.path.basename(self.path),self.start_error_message)selenium.common.exceptions.WebDriverException:消息:'geckodriver'可执行文件需要在PATH中。

If you call a selenium driver without any arguments, the path to the webdriver executable must be in the system PATH environment variables. 如果调用不带任何参数的selenium驱动程序,则webdriver可执行文件的路径必须位于系统PATH环境变量中。

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 geckodriver is an additional component that you must install to control Firefox. geckodriver是您必须安装以控制Firefox的附加组件。 It's not included with the selenium package, so it must be installed separately. 它不包含在selenium包中,因此必须单独安装。

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: 以下shell脚本将从Mozilla的repo下载最新的geckodriver并将其放在usr/local/bin ,因此可以在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 ) (通过pipeasy_install安装selenium后运行此脚本)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM