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


当前回答

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

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

其他回答

“geckodriver”可执行文件需要在PATH中。

首先,您需要从这里下载最新的可执行geckodriver,以使用Selenium运行最新的Firefox

实际上,Selenium客户端绑定试图从系统PATH中定位geckodriver可执行文件。您需要将包含可执行文件的目录添加到系统路径中。

On Unix systems you can do the following to append it to your system’s search path, if you’re using a Bash-compatible shell: export PATH=$PATH:/path/to/directory/of/executable/downloaded/in/previous/step On Windows you will need to update the Path system variable to add the full directory path to the executable geckodriver manually or command line** (don't forget to restart your system after adding executable geckodriver into system PATH to take effect)**. The principle is the same as on Unix.

现在你可以像下面这样运行你的代码:-

from selenium import webdriver

browser = webdriver.Firefox()

webdriverexception:消息:预期的浏览器二进制位置,但无法在默认位置找到二进制,没有'moz:firefoxOptions。提供Binary '功能,并且没有在命令行上设置二进制标志

该异常清楚地指出,您已经在其他位置安装了Firefox,而Selenium正试图从默认位置找到Firefox并启动,但它无法找到它。您需要显式提供Firefox安装的二进制位置来启动Firefox,如下所示

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

binary = FirefoxBinary('path/to/installed firefox binary')
browser = webdriver.Firefox(firefox_binary=binary)

https://github.com/mozilla/geckodriver/releases

Windows:

从GitHub下载文件,提取它,并粘贴到Python文件。这对我很管用。

https://github.com/mozilla/geckodriver/releases

对我来说,我的路径是:

C:\Users\MYUSERNAME\AppData\Local\Programs\Python\Python39

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

这个步骤在Ubuntu和Firefox 50上为我解决了这个问题。

下载geckodriver 拷贝geckodriver到/usr/local/bin文件夹

不需要添加:

firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
firefox_capabilities['binary'] = '/usr/bin/firefox'
browser = webdriver.Firefox(capabilities=firefox_capabilities)

如果你正在使用Anaconda,你所要做的就是激活你的虚拟环境,然后使用以下命令安装geckodriver:

    conda install -c conda-forge geckodriver

在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