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


当前回答

默认不安装Geckodriver。

geckodriver

输出:

Command 'geckodriver' not found, but it can be installed with:

sudo apt install firefox-geckodriver

下面的命令不仅安装它,而且还将它放在可执行PATH中。

sudo apt install firefox-geckodriver

这个问题只需要一步就解决了。我和你有完全一样的错误,我一安装它就消失了。去吧,试一试。

which geckodriver

输出:

/usr/bin/geckodriver

geckodriver

输出:

1337    geckodriver    INFO    Listening on 127.0.0.1:4444
^C

其他回答

如果您在Linux上,可以使用一个简单的命令来解决这个问题

首先,下载(https://github.com/mozilla/geckodriver/releases)并解压ZIP文件 打开解压的文件夹 从文件夹(解压后geckodriver文件所在的文件夹)打开终端 现在在您的终端上运行这个简单的命令,将geckodriver复制到正确的文件夹中: Sudo cp geckodriver /usr/local/bin

Mac的步骤

简单的解决方案是下载GeckoDriver并将其添加到您的系统PATH中。你可以使用以下两种方法中的任何一种:

短的方法

下载并解压Geckodriver。 在启动驱动程序时提到路径: firefox (executable_path='/your/path/to/geckodriver')

长方法

下载并解压Geckodriver。 . bash_profile开放。如果您还没有创建它,您可以使用命令:touch ~/.bash_profile。然后使用open ~/.bash_profile打开它 考虑到GeckoDriver文件存在于你的Downloads文件夹中,你可以在.bash_profile文件中添加以下代码行: 路径= " /用户/ <你的名字> /下载/ geckodriver:美元路径” 导出路径

这样你就把GeckoDriver的路径附加到系统路径中了。这将告诉系统在执行Selenium脚本时GeckoDriver的位置。

保存.bash_profile并强制执行。这会立即加载值,而不必重新启动。您可以执行以下命令:

源~ / . bash_profile

就是这样。你完蛋了!现在可以运行Python脚本了。

确保您有正确版本的驱动程序(geckodriver), x86或64。 确保您正在检查正确的环境。例如,作业在Docker容器中运行,而环境在主机操作系统上检查。

这为我解决了问题。

from selenium import webdriver
driver = webdriver.Firefox(executable_path=r'your\path\geckodriver.exe')
driver.get('http://inventwithpython.com')

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

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