我正在用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就可以了。我只是更新了火狐浏览器。

下面是我使用的代码:

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 18.04+和最新发布的geckodriver

这应该也适用于其他类unix的变种。

export GV=v0.30.0
wget "https://github.com/mozilla/geckodriver/releases/download/$GV/geckodriver-$GV-linux64.tar.gz"
tar xvzf geckodriver-$GV-linux64.tar.gz
chmod +x geckodriver
sudo cp geckodriver /usr/local/bin/

Mac更新为:

geckodriver-$GV-macos.tar.gz

在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的回答?

如果要在Windows 10系统下添加驱动程序路径:

右键单击“这台PC”图标,选择“属性” 点击“高级系统设置” 点击屏幕底部的“环境变量” 在“用户变量”部分选中“路径”,然后点击“编辑” 通过点击“New”,输入你要添加的驱动程序的路径,然后按回车键,来添加变量的路径。 输入路径后,点击“确定” 继续点击“确定”,直到关闭所有屏幕

如果你使用虚拟环境和Windows 10(可能其他系统也一样),你只需要把geckodriver.exe放到虚拟环境目录下的以下文件夹中:

... my_virtual_env_directory \ \ geckodriver脚本。exe

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

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