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


当前回答

我用的是Windows 10,这对我来说很管用:

从这里下载geckodriver。为您使用的计算机下载正确的版本。 解压缩刚刚下载的文件,并剪切/复制其中包含的“。exe”文件 导航到C:{你的python根文件夹}。我的是C:\Python27。将geckodriver.exe文件粘贴到此文件夹中。 重新启动开发环境。 尝试再次运行代码。现在应该可以了。

其他回答

此错误消息…

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/')

在已经安装了Homebrew的macOS上,您可以简单地运行Terminal命令:

brew install geckodriver

因为Homebrew已经扩展了PATH,所以不需要修改任何启动脚本。

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

使用conda install -c conda-forge geckodriver,你不需要在路径中添加任何东西,也不需要编辑代码!

最简单的方法为Windows!

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

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

brew install geckodriver

代码并没有改变:

from selenium import webdriver
browser = webdriver.Firefox()