使用 Python 的 Selenium - Geckodriver 可执行文件需要在 PATH 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40208051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selenium using Python - Geckodriver executable needs to be in PATH
提问by tadm123
I'm new to programming and started with Python
about 2 months ago and am going over Sweigart's Automate the Boring Stuff with Pythontext. I'm using IDLE and already installed the selenium module and the Firefox browser.
Whenever I tried to run the webdriver function, I get this:
我是编程新手,Python
大约 2 个月前开始学习,正在阅读 Sweigart 的用 Python文本自动化无聊的东西。我正在使用 IDLE 并且已经安装了 selenium 模块和 Firefox 浏览器。每当我尝试运行 webdriver 功能时,我都会得到以下信息:
from selenium import webdriver
browser = webdriver.Firefox()
Exception :-
例外 :-
Exception ignored in: <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x00000249C0DA1080>>
Traceback (most recent call last):
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 163, in __del__
self.stop()
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 135, in stop
if self.process is None:
AttributeError: 'Service' object has no attribute 'process'
Exception ignored in: <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x00000249C0E08128>>
Traceback (most recent call last):
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 163, in __del__
self.stop()
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 135, in stop
if self.process is None:
AttributeError: 'Service' object has no attribute 'process'
Traceback (most recent call last):
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 64, in start
stdout=self.log_file, stderr=self.log_file)
File "C:\Python\Python35\lib\subprocess.py", line 947, in __init__
restore_signals, start_new_session)
File "C:\Python\Python35\lib\subprocess.py", line 1224, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
browser = webdriver.Firefox()
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 135, in __init__
self.service.start()
File "C:\Python\Python35\lib\site-packages\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.
I think I need to set the path for geckodriver
but not sure how, so can anyone tell me how would I do this?
我想我需要设置路径geckodriver
但不确定如何设置,所以谁能告诉我我将如何执行此操作?
采纳答案by Saurabh Gaur
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
selenium.common.exceptions.WebDriverException: 消息:'geckodriver' 可执行文件需要在 PATH 中。
首先,您需要从这里下载最新的可执行文件 geckodriver 以使用 selenium 运行最新的 firefox
Actually The Selenium client bindings tries to locate the geckodriver
executable from the system PATH
. You will need to add the directory containing the executable to the system path.
实际上 Selenium 客户端绑定试图geckodriver
从系统中定位可执行文件PATH
。您需要将包含可执行文件的目录添加到系统路径中。
On Unix systems you can do the following to append it to your system's search path, if you're using a bash-compatible shell:
export PATH=$PATH:/path/to/directory/of/executable/downloaded/in/previous/step
On Windows you will need to update the Path system variable to add the full directory path to the executable geckodrivermanuallyor command line(don't forget to restart your system after adding executable geckodriver into system PATH to take effect). The principle is the same as on Unix.
在 Unix 系统上,如果您使用与 bash 兼容的 shell,您可以执行以下操作将其附加到系统的搜索路径:
export PATH=$PATH:/path/to/directory/of/executable/downloaded/in/previous/step
在 Windows 上,您需要更新Path 系统变量以手动或命令行将完整目录路径添加到可执行 geckodriver (在将可执行 geckodriver 添加到系统 PATH 后不要忘记重新启动系统以使其生效)。原理与Unix相同。
Now you can run your code same as you're doing as below :-
现在你可以像下面一样运行你的代码:-
from selenium import webdriver
browser = webdriver.Firefox()
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
selenium.common.exceptions.WebDriverException:消息:预期的浏览器二进制位置,但无法在默认位置找到二进制文件,未提供“moz:firefoxOptions.binary”功能,并且命令行上未设置二进制标志
Exception clearly states you have installed firefox some other location while Selenium is trying to find firefox and launch from default location but it couldn't find. You need to provide explicitly firefox installed binary location to launch firefox as below :-
异常清楚地表明您已在其他位置安装了 Firefox,而 Selenium 正在尝试查找 Firefox 并从默认位置启动,但找不到。您需要明确提供安装 Firefox 的二进制位置以启动 Firefox,如下所示:-
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
binary = FirefoxBinary('path/to/installed firefox binary')
browser = webdriver.Firefox(firefox_binary=binary)
回答by Nesa
This solved it for me.
这为我解决了它。
from selenium import webdriver
driver = webdriver.Firefox(executable_path=r'your\path\geckodriver.exe')
driver.get('http://inventwithpython.com')
回答by Andrea Perdicchia
this steps SOLVED for me on ubuntu firefox 50.
这个步骤在 ubuntu firefox 50 上为我解决了。
Download geckodriver
Copy geckodriver in /usr/local/bin
将 geckodriver 复制到 /usr/local/bin
You do NOT need to add
你不需要添加
firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
firefox_capabilities['binary'] = '/usr/bin/firefox'
browser = webdriver.Firefox(capabilities=firefox_capabilities)
回答by Anthon
The answer by @saurabh solves the issue, but doesn't explain why Automate the Boring Stuff with Pythondoesn't include those steps.
@saurabh 的答案解决了这个问题,但没有解释为什么用 Python 自动化无聊的东西不包括这些步骤。
This is caused by the book being based on selenium 2.x and the Firefox driver for that series does not need the gecko driver. The Gecko interface to drive the browser was not available when selenium was being developed.
这是因为本书基于 selenium 2.x 并且该系列的 Firefox 驱动程序不需要 gecko 驱动程序。在开发 selenium 时,驱动浏览器的 Gecko 接口不可用。
The latest versionin the selenium 2.x series is 2.53.6 (see e.g this answers, for an easier view of the versions).
selenium 2.x 系列的最新版本是 2.53.6(参见例如这个答案,以便更轻松地查看版本)。
The 2.53.6 version pagedoesn't mention gecko at all. But since version 3.0.2 the documentation explicitly statesyou need to install the gecko driver.
在2.53.6版本页面完全不提壁虎。但是从 3.0.2 版本开始,文档明确指出您需要安装 gecko 驱动程序。
If after an upgrade (or install on a new system), your software that worked fine before (or on your old system) doesn't work anymore and you are in a hurry, pin the selenium version in your virtualenv by doing
如果在升级(或安装在新系统上)之后,您之前(或在旧系统上)运行良好的软件不再运行,并且您很着急,请通过执行以下操作将 selenium 版本固定在您的 virtualenv 中
pip install selenium==2.53.6
but of course the long term solution for development is to setup a new virtualenv with the latest version of selenium, install the gecko driver and test if everything still works as expected. But the major version bump might introduce other API changes that are not covered by your book, so you might want to stick with the older selenium, until you are confident enough that you can fix any discrepancies between the selenium2 and selenium3 API yourself.
但当然,开发的长期解决方案是使用最新版本的 selenium 设置新的 virtualenv,安装 gecko 驱动程序并测试一切是否仍按预期工作。但是主要版本的变化可能会引入您的书中未涵盖的其他 API 更改,因此您可能希望坚持使用较旧的 selenium,直到您有足够的信心可以自己修复 selenium2 和 selenium3 API 之间的任何差异。
回答by roskakori
On macOS with Homebrewalready installed you can simply run the Terminal command
在已经安装Homebrew 的macOS 上,您可以简单地运行终端命令
$ brew install geckodriver
Because homebrew already did extend the PATH
there's no need to modify any startup scripts.
因为自制软件已经扩展了,PATH
所以不需要修改任何启动脚本。
回答by Ripon Al Wasim
To set up geckodriver for Selenium Python:
为 Selenium Python 设置 geckodriver:
It needs to set geckodriver path with FirefoxDriver as below code:
它需要使用 FirefoxDriver 设置 geckodriver 路径,如下代码:
self.driver = webdriver.Firefox(executable_path = 'D:\Selenium_RiponAlWasim\geckodriver-v0.18.0-win64\geckodriver.exe')
Download geckodriver for your suitable OS (from https://github.com/mozilla/geckodriver/releases) -> Extract it in a folder of your choice -> Set the path correctly as mentioned above
下载适用于您合适操作系统的 geckodriver(来自https://github.com/mozilla/geckodriver/releases)-> 将其解压缩到您选择的文件夹中-> 如上所述正确设置路径
I'm using Python 3.6.2 and Selenium WebDriver 3.4.3 in Windows 10.
我在 Windows 10 中使用 Python 3.6.2 和 Selenium WebDriver 3.4.3。
Another way to set up geckodriver:
另一种设置 geckodriver 的方法:
i) Simply paste the geckodriver.exe under /Python/Scripts/ (In my case the folder was: C:\Python36\Scripts)
ii) Now write the simple code as below:
i) 只需将 geckodriver.exe 粘贴到 /Python/Scripts/ 下(在我的情况下,文件夹是:C:\Python36\Scripts)
ii) 现在编写如下简单代码:
self.driver = webdriver.Firefox()
回答by Rodolfo Alvarez
回答by jmunsch
Ubuntu 18.04+ and Newest release of geckodriver
Ubuntu 18.04+ 和最新版本的 geckodriver
This should also work for other *nix varieties as well.
这也适用于其他 *nix 品种。
export GV=v0.26.0
wget "https://github.com/mozilla/geckodriver/releases/download/$GV/geckodriver-$GV-linux64.tar.gz"
tar xvzf geckodriver-$GV-linux64.tar.gz
chmod +x geckodriver
sudo cp geckodriver /usr/local/bin/
For mac update to:
对于 mac 更新到:
geckodriver-$GV-macos.tar.gz
回答by Navarasu
I see the discussions still talk about the old way of setting up geckodriver by downloading the binary and configuring the path manually.
我看到讨论仍在讨论通过下载二进制文件并手动配置路径来设置 geckodriver 的旧方法。
This can be done automatically using webdriver-manager
这可以使用webdriver-manager自动完成
pip install webdriver-manager
Now the above code in the question will work simply with below change,
现在问题中的上述代码将简单地与以下更改一起使用,
from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager
driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
回答by Jalles10
The easiest way for windows!
Download the latest version of geckodriver
from here. Add the geckodriver.exefile to the python directory (or any other directory which already in PATH
). This should solve the problem (Tested on Windows 10)
最简单的 Windows 方法!从这里
下载最新版本。将geckodriver.exe文件添加到 python 目录(或已经在. 这应该可以解决问题(在 Windows 10 上测试)geckodriver
PATH