我正在用Python文本阅读Sweigart的《自动化无聊的东西》。我正在使用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

在处理上述异常时,发生了另一个异常:

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设置路径,但我不确定如何,所以我该如何做到这一点?


当前回答

在树莓派上,我必须从ARM驱动程序创建它,并在webdriver.py文件中设置geckodriver和日志路径:

sudo nano /usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py
def __init__(self, firefox_profile=None, firefox_binary=None,
             timeout=30, capabilities=None, proxy=None,
             executable_path="/PATH/gecko/geckodriver",
             firefox_options=None,
             log_path="/PATH/geckodriver.log"):

其他回答

如果你使用虚拟环境和Windows 10(可能其他系统也一样),你只需要把geckodriver.exe放到虚拟环境目录下的以下文件夹中:

... my_virtual_env_directory \ \ geckodriver脚本。exe

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

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

pip install webdriver-manager

现在问题中的上面代码将简单地与下面的更改一起工作,

from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager

driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())

对我来说,在相同的环境中安装geckodriver就足够了:

brew install geckodriver

代码并没有改变:

from selenium import webdriver
browser = webdriver.Firefox()

最简单的方法为Windows!

从这里下载最新版本的geckodriver。将geckodriver.exe文件添加到Python目录(或PATH中已经存在的任何其他目录)。这应该能解决问题(在Windows 10上进行了测试)。

此错误消息…

FileNotFoundError: [WinError 2] The system cannot find the file specified

...暗示您的程序无法定位指定的文件,并且在处理异常时发生了以下异常:

selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.

... 这意味着你的程序在启动/生成一个新的浏览上下文(即Firefox浏览器会话)的过程中无法定位GeckoDriver。


你可以从mozilla / GeckoDriver下载最新的GeckoDriver, unzip/untar并将GeckoDriver二进制文件/可执行文件存储在你系统中的任何位置,通过关键executable_path传递GeckoDriver的绝对路径,如下所示:

from selenium import webdriver

driver = webdriver.Firefox(executable_path='/path/to/geckodriver')
driver.get('http://google.com/')

如果firefox没有安装在默认位置(即安装在一个自定义位置),另外你需要通过属性binary_location传递firefox二进制文件的绝对路径,如下所示:

# An Windows example
from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.binary_location = r'C:\Program Files\Mozilla Firefox\firefox.exe'
driver = webdriver.Firefox(firefox_options=options, executable_path=r'C:\WebDrivers\geckodriver.exe')
driver.get('http://google.com/')