首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Python的Selenium - Geckodriver可执行文件需要在路径中。

使用Python的Selenium - Geckodriver可执行文件需要在路径中。
EN

Stack Overflow用户
提问于 2016-10-23 21:39:13
回答 34查看 906.4K关注 0票数 619

我对编程很陌生,大约两个月前就开始使用Python了,我正在研究Sweigart用Python文本自动处理无聊的东西。我正在使用空闲,并且已经安装了Selenium模块和Firefox浏览器。

每当我尝试运行webdriver函数时,我都会得到以下内容:

代码语言:javascript
运行
复制
from selenium import webdriver
browser = webdriver.Firefox()

例外:

代码语言:javascript
运行
复制
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

在处理上述异常的过程中,发生了另一个异常:

代码语言:javascript
运行
复制
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设置路径,但我不确定如何设置,那么我将如何做呢?

EN

回答 34

Stack Overflow用户

回答已采纳

发布于 2016-10-23 23:16:01

selenium.common.exceptions.WebDriverException:消息:“geckodriver”可执行文件需要在路径中。

首先,您需要从这里下载最新的可执行geckodriver,以便使用Selenium运行最新的Firefox。

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

  • 在Unix系统上,如果使用的是与Bash兼容的shell,则可以执行以下操作将其附加到系统的搜索路径: 导出PATH=$PATH:/path/to/directory/of/executable/downloaded/in/previous/step
  • 在Windows上,您需要更新路径系统变量,以便将完整的目录路径添加到可执行的geckodriver、手动命令行** (不要忘记在将可执行的geckodriver添加到系统路径后重新启动系统)**。原理与Unix相同。

现在您可以运行代码,如下所示:-

代码语言:javascript
运行
复制
from selenium import webdriver

browser = webdriver.Firefox()

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

在Selenium试图找到Firefox并从默认位置启动时,异常明显地表明您已经安装了Firefox的其他位置,但是它找不到它。您需要显式地提供Firefox安装的二进制位置,以启动Firefox,如下所示:

代码语言:javascript
运行
复制
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)

https://github.com/mozilla/geckodriver/releases

适用于Windows:

从GitHub下载文件,解压缩文件,并将其粘贴到Python中。对我起作用了。

https://github.com/mozilla/geckodriver/releases

对我来说,我的道路是:

代码语言:javascript
运行
复制
C:\Users\MYUSERNAME\AppData\Local\Programs\Python\Python39
票数 450
EN

Stack Overflow用户

发布于 2017-02-08 19:45:54

这为我解决了问题。

代码语言:javascript
运行
复制
from selenium import webdriver
driver = webdriver.Firefox(executable_path=r'your\path\geckodriver.exe')
driver.get('http://inventwithpython.com')
票数 221
EN

Stack Overflow用户

发布于 2016-12-02 12:09:23

这个步骤在Ubuntu和Firefox 50上为我解决了这个问题。

  1. 下载壁虎司机
  2. 将geckodriver复制到文件夹/usr/local/bin

您不需要添加:

代码语言:javascript
运行
复制
firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
firefox_capabilities['binary'] = '/usr/bin/firefox'
browser = webdriver.Firefox(capabilities=firefox_capabilities)
票数 145
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40208051

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档