当前位置: 首页 > 面试题库 >

使用Python的Selenium-Geckodriver可执行文件必须位于PATH中

姬裕
2023-03-14
问题内容

我正在使用IDLE,并且已经安装了selenium模块和Firefox浏览器。每当我尝试运行webdriver函数时,都会得到以下信息:

from selenium import webdriver
browser = webdriver.Firefox()

例外:

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. 

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


问题答案:

selenium.common.exceptions.WebDriverException:消息:“ geckodriver”可执行文件必须位于PATH中。

首先,你需要从此处下载最新的可执行geckodriver,以使用硒运行最新的Firefox

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

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

export PATH=$PATH:/path/to/directory/of/executable/downloaded/in/previous/step

在Windows上,你将需要更新Path系统变量以 手动或命令行将完整目录路径添加到可执行geckodriver (不要忘记在将可执行geckodriver添加到系统PATH中生效后重新启动系统)。其原理与Unix相同。

现在,你可以按照以下步骤运行代码:-

from selenium import webdriver

browser = webdriver.Firefox()

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

异常清楚地表明你在Selenium试图查找Firefox并从默认位置启动时在其他位置安装了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)


 类似资料:
  • 问题内容: 我是编程的新手,大约2个月前开始使用Python,现在正在研究Sweigart的《用Python文本自动完成无聊的事情》。我正在使用Spyder 3,并且已经安装了selenium模块和Firefox浏览器。我在python文件中使用了以下代码 我收到此错误: 除了进入终端并使用进行安装外,我还下载了geckodriver.exe。 奇怪的是,如果我进入终端并输入“ python”,然

  • 问题内容: 试图让Firefox在spyder中使用硒运行。我当前的代码是 我仍然收到此错误: 即使gekodriver在该文件夹中。 证明gekodriver位置正确 我也尝试过将路径导出到〜。/ bash_profile,现在看起来像这样。 我还在spyder中设置了当前路径: gekodriver在其中 我到处都看过,大多数人说如果它位于/ usr / local / bin中,它应该自动运

  • 问题内容: 错误信息: “ chromedriver”可执行文件必须位于PATH中 我试图在pycharm中使用Selenium编写脚本,但是发生了以上错误。我已经将selenium链接到pycharm上(如此处所示)(最新)。 我是selenium的新手,不是“ selenium”文件夹中的chromedriver。如果不是,我在哪里可以找到它并将其添加到路径中? 顺便说一句,我尝试在cmd中键

  • 我是编程新手,大约两个月前开始使用Python,现在正在复习Sweigart的“用Python文本自动处理无聊的东西”。我正在使用IDLE并且已经安装了Selenium模块和Firefox浏览器。

  • 问题内容: 我正在尝试运行此脚本: https://github.com/Chillee/coursera-dl- all 但是,脚本在此行失败,并显示以下错误 如何将phantomjs添加到PATH?我正在运行ubuntu 16.04并通过安装selenium。 问题答案: 我在命令行中使用此命令解决了同样的问题 如果/ home / login / phantomjs / bin是带有可执行文

  • 我非常熟悉在selenium中使用chromedriver,我现在尝试使用geckdriver,但由于某些原因,我不断收到错误信息,geckodriver可执行文件需要在PATH中 我使用Python遵循了Selenium中的步骤-Geckodriver可执行文件需要在PATH中 > 我在环境变量中的path中添加了gecko驱动程序 我将firefox更新为最新版本 我用了二进制方法 把壁虎司机