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

我是编程新手,大约两个月前开始使用 "Python",现在正在翻阅Sweigart的Automate the Boring Stuff with Python文本。我使用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,以便使用selenium运行最新的firefox

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

  • 在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'功能,也没有在命令行中设置二进制标志。

异常情况清楚地表明你在其他地方安装了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)
评论(14)

这个步骤为我解决了ubuntu火狐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)
评论(6)

令人遗憾的是,没有一本关于Selenium/Python的书,以及通过Google对这个问题的大多数评论都没有清楚地解释在Mac上设置这个的路径逻辑(一切都在Windows!!!!)。YouTube上的内容都是在你设置好路径后才开始的(在我看来,这是很便宜的方法!)。所以,对于你们这些优秀的Mac用户来说,使用以下方法来编辑你的bash路径文件。

>$touch ~/.bash_profile; open ~/.bash_profile.

然后添加一个类似这样的路径.... *# 为geckodriver设置PATH PATH="/usr/bin/geckodriver:${PATH}" 输出PATH

为Selenium firefox设置PATH

PATH="~/Users/yourNamePATH/VEnvPythonInterpreter/lib/python2.7/Site-packages/selenium/webdriver/firefox/:${PATH}" 输出PATH

为firefox驱动上的可执行程序设置PATH

PATH="/Users/yournamePATH/VEnvPythonInterpreter/lib/python2.7/Site-packages/selenium/webdriver/common/service.py:${PATH}" 出口PATH*。

这对我来说是有效的。我担心的是,Selenium Windows社区什么时候才能开始玩真正的游戏,把我们这些Mac用户纳入他们傲慢的俱乐部成员。

评论(0)