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


当前回答

为Selenium Python设置geckodriver:

它需要用FirefoxDriver设置geckodriver路径,如下所示:

self.driver = webdriver.Firefox(executable_path = 'D:\Selenium_RiponAlWasim\geckodriver-v0.18.0-win64\geckodriver.exe')

下载适合您操作系统的geckodriver(从https://github.com/mozilla/geckodriver/releases)→将其解压到您选择的文件夹中→如上所述正确设置路径。

我在Windows 10上使用的是Python 3.6.2和Selenium WebDriver 3.4.3。

另一种设置geckodriver的方法:

i)简单地粘贴geckodriver.exe在/Python/Scripts/(在我的情况下,文件夹是:C:\Python36\Scripts) ii)现在编写如下简单代码:

self.driver = webdriver.Firefox()

其他回答

默认不安装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

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

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

在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

考虑安装一个容器化的Firefox:

docker pull selenium/standalone-firefox
docker run --rm -d -p 5555:4444 --shm-size=2g selenium/standalone-firefox

使用webdriver连接。远程:

driver = webdriver.Remote('http://localhost:5555/wd/hub', DesiredCapabilities.FIREFOX)
driver.set_window_size(1280, 1024)
driver.get('https://toolbox.googleapps.com/apps/browserinfo/')
driver.save_screenshot('info.png')

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。