使用Python的Selenium - Geckodriver可执行文件需要在PATH中

tad*_*123 380 python firefox selenium selenium-firefoxdriver geckodriver

我是编程的新手,Python大约2个月前开始使用Python文本,正在浏览Sweigart的自动化无聊的东西.我正在使用IDLE并且已经安装了selenium模块和Firefox浏览器.每当我尝试运行webdriver函数时,我都会得到:

from selenium import webdriver
browser = webdriver.Firefox()
Run Code Online (Sandbox Code Playgroud)

例外: -

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. 
Run Code Online (Sandbox Code Playgroud)

我想我需要设置路径geckodriver但不确定如何,所以有人能告诉我我该怎么做?

Sau*_*aur 312

selenium.common.exceptions.WebDriverException:消息:'geckodriver'可执行文件需要在PATH中.

首先,您需要从此处下载最新的可执行geckodriver以使用selenium运行最新的firefox

实际上Selenium客户端绑定试图geckodriver从系统中找到可执行文件PATH.您需要将包含可执行文件的目录添加到系统路径.

  • 在Unix系统上,如果您使用的是与bash兼容的shell,则可以执行以下操作将其附加到系统的搜索路径:

    export PATH=$PATH:/path/to/directory/of/executable/downloaded/in/previous/step
    
    Run Code Online (Sandbox Code Playgroud)
  • 在Windows上,您需要更新Path系统变量以 手动命令行添加可执行geckodriver 的完整目录路径(在将可执行geckodriver添加到系统PATH中后,不要忘记重新启动系统才能生效).原理与Unix相同.

现在,您可以按照以下方式运行代码: -

from selenium import webdriver

browser = webdriver.Firefox()
Run Code Online (Sandbox Code Playgroud)

selenium.common.exceptions.WebDriverException:消息:预期的浏览器二进制位置,但无法在默认位置找到二进制文件,未提供'moz:firefoxOptions.binary'功能,并且在命令行上未设置二进制标志

异常清楚地表明你已经安装了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)
Run Code Online (Sandbox Code Playgroud)

  • 除了这个答案,我想扩展在unix环境中设置`PATH`.您可以在代码中设置它,因为您不需要系统范围:`os.environ ["PATH"] + = os.pathsep +'path/to/dir/containing/geckodriver /'`或者只是保留geckodriver二进制文件在路径中已有的目录中:`mv geckodriver/usr/local/bin` (11认同)
  • 谢谢,但我在`C:\ Python\Python35\selenium`目录上设置了我的`geckodriver.exe`,我设置了你描述的路径,但它给出了我的错误如下: (5认同)
  • 谢谢@Saurabh Gaur,它现在正在运作.我手动将Firefox的路径添加到系统变量中,并且一切正常.需要一点时间才能启动,但我猜这是正常的.谢谢! (3认同)
  • 当我开始指定firefox二进制路径时,我收到错误"WebDriverException:消息:无法启动浏览器:权限被拒绝",但重新启动计算机(Windows 10)解决了问题. - 以防万一其他人和我一样遇到同样的问题. (3认同)
  • 什么是二进制文件?这是指可执行文件吗? (2认同)

Nes*_*esa 126

这解决了我.

from selenium import webdriver
driver = webdriver.Firefox(executable_path=r'your\path\geckodriver.exe')
driver.get('http://inventwithpython.com')
Run Code Online (Sandbox Code Playgroud)

  • 它适用于Windows.这是最简单的答案. (6认同)

And*_*hia 114

这个步骤在ubuntu firefox 50上为我解决了.

  1. 下载geckodriver

  2. 在/ usr/local/bin中复制geckodriver

你不需要添加

firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
firefox_capabilities['binary'] = '/usr/bin/firefox'
browser = webdriver.Firefox(capabilities=firefox_capabilities)
Run Code Online (Sandbox Code Playgroud)


Nav*_*asu 32

我看到讨论仍在讨论通过下载二进制文件并手动配置路径来设置 geckodriver 的旧方法。

这可以使用webdriver-manager自动完成

pip install webdriver-manager
Run Code Online (Sandbox Code Playgroud)

现在问题中的上述代码将简单地与以下更改一起使用,

from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager

driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
Run Code Online (Sandbox Code Playgroud)

  • 惊人的!这容易多了! (2认同)

Ant*_*hon 29

@saurabh的答案解决了这个问题,但没有解释为什么Automate with Boring Stuff with Python不包括这些步骤.

这是因为本书基于selenium 2.x而且该系列的Firefox驱动程序不需要gecko驱动程序.在开发selenium时,用于驱动浏览器的Gecko界面不可用.

selenium 2.x系列中的最新版本是2.53.6(参见此答案,以便更轻松地查看版本).

2.53.6版本页面完全不提壁虎.但是从版本3.0.2开始,文档明确指出您需要安装gecko驱动程序.

如果在升级之后(或安装在新系统上),你的软件在之前(或在你的旧系统上)运行良好的软件不再起作用并且你很匆忙,那就把你的virtualenv中的selenium版本固定下来

pip install selenium==2.53.6
Run Code Online (Sandbox Code Playgroud)

但当然,开发的长期解决方案是使用最新版本的selenium设置新的virtualenv,安装gecko驱动程序并测试一切是否仍然按预期工作.但主要的版本可能会引入您的书中未涵盖的其他API更改,因此您可能希望坚持使用较旧的selenium,直到您有足够的信心可以自行修复selenium2和selenium3 API之间的任何差异.

  • 我希望这是最好的答案 (2认同)

ros*_*ori 18

在安装了Homebrew的 macOS上,您只需运行Terminal命令即可

$ brew install geckodriver
Run Code Online (Sandbox Code Playgroud)

因为自制软件已经扩展了,PATH所以不需要修改任何启动脚本.

  • @roskakori我这样做了并且安装成功,但我仍然遇到相同的错误 (2认同)

Rip*_*sim 15

为Selenium Python设置geckodriver:

它需要使用FirefoxDriver设置geckodriver路径,如下代码:

self.driver = webdriver.Firefox(executable_path = 'D:\Selenium_RiponAlWasim\geckodriver-v0.18.0-win64\geckodriver.exe')
Run Code Online (Sandbox Code Playgroud)

下载geckodriver以获得合适的操作系统(来自https://github.com/mozilla/geckodriver/releases) - >将其解压缩到您选择的文件夹中 - >如上所述正确设置路径

我在Windows 10中使用Python 3.6.2和Selenium WebDriver 3.4.3.

另一种设置geckodriver的方法:

i)只需将geckodriver.exe粘贴到/ Python/Scripts /下(在我的情况下文件夹是:C:\ Python36\Scripts)
ii)现在编写简单的代码如下:

self.driver = webdriver.Firefox()
Run Code Online (Sandbox Code Playgroud)


小智 10

对于Ubuntu 16.04 (Xenial Xerus) 及更高版本,您可以执行以下操作:

对于火狐浏览器:
sudo apt-get install firefox-geckodriver

对于 Chrome:
sudo apt-get install chromium-chromedriver


Uma*_*sai 8

MAC的步骤:

简单的解决方案是下载GeckoDriver并将其添加到您的系统PATH中.您可以使用以下两种方法之一:

简短方法:

1)下载并解压缩Geckodriver.

2)启动驱动程序时提到路径:

driver = webdriver.Firefox(executable_path='/your/path/to/geckodriver')
Run Code Online (Sandbox Code Playgroud)

长方法:

1)下载并解压缩Geckodriver.

2)打开.bash_profile.如果您尚未创建它,可以使用以下命令执行此操作:touch ~/.bash_profile.然后打开它:open ~/.bash_profile

3)考虑到下载文件夹中存在GeckoDriver文件,您可以将以下行添加到.bash_profile文件中:

PATH="/Users/<your-name>/Downloads/geckodriver:$PATH"
export PATH
Run Code Online (Sandbox Code Playgroud)

通过这个,您将GeckoDriver的路径附加到您的系统路径.这告诉系统在执行Selenium脚本时GeckoDriver的位置.

4)保存.bash_profile并强制执行.这会立即加载值而无需重新启动.为此,您可以运行以下命令:

source ~/.bash_profile

5)就是这样.你完成了!您现在可以运行Python脚本.

  • 我能够使用 Homebrew 下载 `geckodriver`:`brew install geckodriver`,然后通过以下方式启动 Firefox:`driver = webdriver.Firefox(executable_path="/usr/local/bin/geckodriver")` (2认同)

Rod*_*rez 8

如果您使用的是Anaconda,那么您要做的就是激活虚拟环境,然后使用以下命令安装geckodriver

    conda install -c conda-forge geckodriver
Run Code Online (Sandbox Code Playgroud)


jmu*_*sch 8

Ubuntu 18.04+和最新版本的geckodriver

这也应该适用于其他* 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/
Run Code Online (Sandbox Code Playgroud)

对于Mac,请更新至:

geckodriver-$GV-macos.tar.gz
Run Code Online (Sandbox Code Playgroud)


小智 7

Windows的最简单方法!此处
下载最新版本。将geckodriver.exe文件添加到python目录(或已存在的任何其他目录)中。这应该可以解决问题(在Windows 10上测试)geckodriverPATH


Sni*_*pro 6

为此线程的未来读者提供一些额外的输入/说明:

以下足以作为Windows 7,Python 3.6,selenium 3.11的分辨率:

@ dsalaj在早期针对Unix的这个主题中的注释也适用于Windows; 修补PATH环境.Windows级别的变量可以避免重启Windows系统.

(1)下载geckodriver(如前面这个帖子中所述)并将(解压缩的)geckdriver.exe放在X:\ Folder\of\your\choice中

(2)Python代码示例:

import os;
os.environ["PATH"] += os.pathsep + r'X:\Folder\of\your\choice';

from selenium import webdriver;
browser = webdriver.Firefox();
browser.get('http://localhost:8000')
assert 'Django' in browser.title
Run Code Online (Sandbox Code Playgroud)

注意:(1)上述代码可能需要大约10秒才能打开指定网址的Firefox浏览器.
(2)如果没有服务器已经在指定的URL上运行或者服务于包含字符串'Django'的页面,python控制台将显示以下错误:selenium.common.exceptions.WebDriverException:消息:已达到错误页面:about :neterror E = connectionFailure&U = HTTP%3A //本地主机%3A8000 /&C = UTF-8&F =定期&d =火狐%20can%E2%80%9


小智 6

geckodriver 默认情况下未安装。

$ geckodriver

Command 'geckodriver' not found, but it can be installed with:

sudo apt install firefox-geckodriver

$
Run Code Online (Sandbox Code Playgroud)

以下命令不仅安装它,而且还将它放在可执行文件中PATH

sudo apt install firefox-geckodriver
Run Code Online (Sandbox Code Playgroud)

只需一步即可解决问题。我和你有完全一样的错误,我一安装它就消失了。去试试吧。

$ which geckodriver
/usr/bin/geckodriver
$
$ geckodriver
1337    geckodriver    INFO    Listening on 127.0.0.1:4444
^C
Run Code Online (Sandbox Code Playgroud)


Ama*_*cha 6

避免错误的新方法是使用Conda环境。

使用conda install -c conda-forge geckodriver时,您无需向路径添加任何内容或编辑代码!


Roo*_*oor 5

我实际上发现您可以使用最新的 geckodriver 而无需将其放入系统路径中。目前我正在使用

https://github.com/mozilla/geckodriver/releases/download/v0.12.0/geckodriver-v0.12.0-win64.zip

火狐 50.1.0

蟒蛇 3.5.2

硒 3.0.2

视窗 10

我正在运行 VirtualEnv(我使用PyCharm管理它,我假设它使用 Pip 来安装所有东西)。

在以下代码中,我可以使用 executable_path 参数为 geckodriver 使用特定路径(我通过查看 Lib\site-packages\selenium\webdriver\firefox\webdriver.py 发现了这一点)。注意我怀疑调用 webdriver 时参数参数的顺序很重要,这就是为什么 executable_path 在我的代码中最后(倒数第二行最右边)。

您可能还注意到我使用自定义 Firefox 配置文件来解决 sec_error_unknown_issuer 问题,如果您正在测试的站点具有不受信任的证书,您将遇到该问题。请参阅如何使用 Selenium 禁用 Firefox 的不可信连接警告?

经过调查发现 Marionette 驱动程序不完整且仍在进行中,并且设置各种功能或配置文件选项以关闭或设置证书都不起作用。所以使用自定义配置文件更容易。

无论如何,这是关于我如何让 geckodriver 在不进入路径的情况下工作的代码:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True

#you probably don't need the next 3 lines they don't seem to work anyway
firefox_capabilities['handleAlerts'] = True
firefox_capabilities['acceptSslCerts'] = True
firefox_capabilities['acceptInsecureCerts'] = True

# In the next line I'm using a specific Firefox profile because
# I wanted to get around the sec_error_unknown_issuer problems with the new Firefox and Marionette driver
# I create a Firefox profile where I had already made an exception for the site I'm testing
# see https://support.mozilla.org/en-US/kb/profile-manager-create-and-remove-firefox-profiles#w_starting-the-profile-manager

ffProfilePath = 'D:\Work\PyTestFramework\FirefoxSeleniumProfile'
profile = webdriver.FirefoxProfile(profile_directory=ffProfilePath)
geckoPath = 'D:\Work\PyTestFramework\geckodriver.exe'
browser = webdriver.Firefox(firefox_profile=profile, capabilities=firefox_capabilities, executable_path=geckoPath)
browser.get('http://stackoverflow.com')
Run Code Online (Sandbox Code Playgroud)


Tan*_*ick 5

如果您使用的是Linux,则可以使用简单的命令来解决此问题

  1. 首先,下载(https://github.com/mozilla/geckodriver/releases)并解压 ZIP 文件

  2. 打开解压后的文件夹

  3. geckodriver从文件夹(解压后文件所在的位置)打开终端

    在此输入图像描述

  4. 现在在终端上运行这个简单的命令,将 geckodriver 复制到正确的文件夹中:

     sudo cp geckodriver /usr/local/bin
    
    Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

458467 次

最近记录:

6 年,3 月 前