我正在用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"):

其他回答

访问Gecko Driver并从下载部分获得Gecko驱动程序的URL。

克隆这个存储库:https://github.com/jackton1/script_install.git

cd script_install

Run

./installer --gecko-driver https://github.com/mozilla/geckodriver/releases/download/v0.18.0/geckodriver-v0.25.0-linux64.tar.gz

一些额外的输入/澄清:

下面是Windows 7、Python 3.6和Selenium 3.11的分辨率:

dsalaj关于Unix的另一个答案的注释也适用于Windows;可以避免在Windows级别上修补PATH环境变量和重新启动Windows系统。

(1)下载geckdriver(如本线程前面所述),并将(解压)geckdriver.exe放在X:\Folder\of\your\choice

(2) Python代码示例:

import os;
os.environ["PATH"] += os.pathsep + r'X:\Folder\of\your\choice';

from selenium import webdriver;
browser = webdriver.Firefox();
browser.get('http://localhost:8000')
assert 'Django' in browser.title

注:

(1)上述代码打开指定URL的Firefox浏览器可能需要大约10秒的时间。

(2)如果没有服务器在指定的URL上运行,或者没有包含字符串'Django'的页面,Python控制台将显示以下错误:

selenium.common.exceptions.WebDriverException: Message: Reached error page: about:neterror?e = connectionFailure&u = http % 3 / / localhost % 3 a8000 / Firefox和c = UTF-8&f = regular&d = % 20可以% E2 % 80% 9

也可以执行echo PATH (Linux),并将geckodriver移动到您喜欢的文件夹。如果目标是系统(而不是虚拟环境)文件夹,则驱动程序将成为全局可访问的。

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

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

Windows用户

使用原始代码,因为它是:

from selenium import webdriver
browser = webdriver.Firefox()
driver.get("https://www.google.com")

然后从mozilla/geckodriver下载驱动

把它放在一个固定的路径(永久地)…作为一个例子,我把它放在:

C: \ Python35

然后进入系统的环境变量。在“系统变量”网格中寻找Path变量并添加:

; C: Python35喝geckodriver

Geckodriver,不是Geckodriver .exe。