我正在用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和Anaconda 2。我尝试设置系统路径变量,但它没有工作。然后我简单地将geckodriver.exe文件添加到Anaconda 2/Scripts文件夹中,现在一切都很好了。

对我来说,这条路是:

C:\Users\Bhavya\Anaconda2\Scripts

其他回答

from webdriverdownloader import GeckoDriverDownloader # vs ChromeDriverDownloader vs OperaChromiumDriverDownloader
gdd = GeckoDriverDownloader()
gdd.download_and_install()
#gdd.download_and_install("v0.19.0")

这将为您提供Windows上gekodriver.exe的路径。

from selenium import webdriver
driver = webdriver.Firefox(executable_path=r'C:\\Users\\username\\\bin\\geckodriver.exe')
driver.get('https://www.amazon.com/')

实际上,我发现你可以使用最新的geckodriver,而不用把它放在系统路径中。目前我正在使用

https://github.com/mozilla/geckodriver/releases/download/v0.12.0/geckodriver-v0.12.0-win64.zip

Firefox 50.1.0

Python 3.5.2

硒3.0.2

Windows 10

我正在运行一个VirtualEnv(我使用PyCharm进行管理,我假设它使用Pip来安装所有内容)。

在下面的代码中,我可以使用executable_path参数为geckodriver使用特定的路径(我通过查看 Lib \网站\ firefox硒\ webdriver \ \ webdriver.py)。注意,我怀疑调用webdriver时参数参数的顺序很重要,这就是为什么executable_path在我的代码中是最后一行(最右边的倒数第二行)。

您可能还注意到,我使用了一个自定义Firefox配置文件来解决sec_error_unknown_issuer问题,如果您正在测试的站点有一个不受信任的证书,您就会遇到这个问题。参见如何使用Selenium禁用Firefox的不受信任连接警告?

经过调查发现,木偶驱动程序是不完整的,仍在进行中,并且没有设置各种功能或配置文件选项来取消或设置证书将会工作。所以使用自定义配置文件更容易。

不管怎样,这里是关于我如何让geckodriver工作而不在路径的代码:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True

#you probably don't need the next 3 lines they don't seem to work anyway
firefox_capabilities['handleAlerts'] = True
firefox_capabilities['acceptSslCerts'] = True
firefox_capabilities['acceptInsecureCerts'] = True

# In the next line I'm using a specific Firefox profile because
# I wanted to get around the sec_error_unknown_issuer problems with the new Firefox and Marionette driver
# I create a Firefox profile where I had already made an exception for the site I'm testing
# see https://support.mozilla.org/en-US/kb/profile-manager-create-and-remove-firefox-profiles#w_starting-the-profile-manager

ffProfilePath = 'D:\Work\PyTestFramework\FirefoxSeleniumProfile'
profile = webdriver.FirefoxProfile(profile_directory=ffProfilePath)
geckoPath = 'D:\Work\PyTestFramework\geckodriver.exe'
browser = webdriver.Firefox(firefox_profile=profile, capabilities=firefox_capabilities, executable_path=geckoPath)
browser.get('http://stackoverflow.com')

在Ubuntu上手动安装geckodriver

访问https://github.com/mozilla/geckodriver/releases 下载最新版本“geckodriver-vX.XX.X-linux64.tar.gz” 解压缩tar -xvzf geckodriver-vX.XX.X-linux64.tar.gz 给geckodriver可执行权限(chmod +x geckodriver) 将geckodriver二进制文件移动到/usr/local/bin或系统路径上的任何位置。

Ubuntu上安装geckodriver的脚本:

#!/bin/bash

INSTALL_DIR="/usr/local/bin"

json=$(curl -s https://api.github.com/repos/mozilla/geckodriver/releases/latest)
url=$(echo "$json" | jq -r '.assets[].browser_download_url | select(contains("linux64"))')
curl -s -L "$url" | tar -xz
chmod +x geckodriver
sudo mv geckodriver "$INSTALL_DIR"
echo "installed geckodriver binary in $INSTALL_DIR"

这个答案完全抄袭自: Corey Goldberg对如何在Ubuntu中安装geckodriver的回答?

Selenium在他们的描述中回答了这个问题。rst文件:

司机 = = = = = = = Selenium需要一个驱动程序来与所选的浏览器交互。Firefox, 例如,需要geckodriver <https://github.com/mozilla/geckodriver/releases>_,在运行下面的示例之前需要安装它。确保它在你的PATH中,例如,将它放在/usr/bin或/usr/local/bin中。

如果没有注意到这一步,将会出现一个错误:selenium.common.exceptions.WebDriverException: Message: 'geckodriver'可执行文件需要在PATH中。

基本上只需要下载geckodriver,解包并将可执行文件移动到/usr/bin文件夹。

在树莓派上,我必须从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"):