我正在用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放到虚拟环境目录下的以下文件夹中:

... my_virtual_env_directory \ \ geckodriver脚本。exe

其他回答

在Windows 10上,我下载geckodriver.exe就可以了。我只是更新了火狐浏览器。

下面是我使用的代码:

from selenium import webdriver
driver = webdriver.Firefox(
    executable_path=r'C:\Users\Usuario\Desktop\Automate the boring stuff with python exercises\Web Scraping\geckodriver.exe')
driver.get('http://inventwithpython.com')

saurabh的回答解决了这个问题,但它没有解释为什么用Python自动化无聊的东西不包括这些步骤。

这是因为本书是基于Selenium 2的。该系列的Firefox驱动程序不需要Gecko驱动程序。在开发Selenium时,驱动浏览器的Gecko界面是不可用的。

Selenium 2的最新版本。X系列是2.53.6(参见例如这些答案,以获得更简单的版本视图)。

2.53.6版本页面完全没有提到Gecko。但是从3.0.2版开始,文档明确指出您需要安装Gecko驱动程序。

如果在升级(或安装到新系统上)之后,以前(或在旧系统上)正常工作的软件不能再工作了,而你又很着急,可以通过以下方法将Selenium版本固定在virtualenv中

pip install selenium==2.53.6

当然,长期的开发解决方案是用最新版本的selenium建立一个新的virtualenv,安装Gecko驱动程序并测试是否一切都能正常工作。

但是主要的版本碰撞可能会引入您的书中没有涉及的其他API更改,因此您可能希望坚持使用旧的Selenium,直到您有足够的信心可以自己修复Selenium 2和Selenium 3 API之间的任何差异。

在macOS v10.12.1 (Sierra)和Python 2.7.10上,这对我来说是有效的:

def download(url):
    firefox_capabilities = DesiredCapabilities.FIREFOX
    firefox_capabilities['marionette'] = True
    browser = webdriver.Firefox(capabilities=firefox_capabilities,
                                executable_path=r'/Users/Do01/Documents/crawler-env/geckodriver')
    browser.get(url)
    return browser.page_source

我用的是Windows 10和Anaconda 2。我尝试设置系统路径变量,但它没有工作。然后我简单地将geckodriver.exe文件添加到Anaconda 2/Scripts文件夹中,现在一切都很好了。

对我来说,这条路是:

C:\Users\Bhavya\Anaconda2\Scripts

一些额外的输入/澄清:

下面是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