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


当前回答

考虑安装一个容器化的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 10系统下添加驱动程序路径:

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

为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()
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/')

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

Mac的步骤

简单的解决方案是下载GeckoDriver并将其添加到您的系统PATH中。你可以使用以下两种方法中的任何一种:

短的方法

下载并解压Geckodriver。 在启动驱动程序时提到路径: firefox (executable_path='/your/path/to/geckodriver')

长方法

下载并解压Geckodriver。 . bash_profile开放。如果您还没有创建它,您可以使用命令:touch ~/.bash_profile。然后使用open ~/.bash_profile打开它 考虑到GeckoDriver文件存在于你的Downloads文件夹中,你可以在.bash_profile文件中添加以下代码行: 路径= " /用户/ <你的名字> /下载/ geckodriver:美元路径” 导出路径

这样你就把GeckoDriver的路径附加到系统路径中了。这将告诉系统在执行Selenium脚本时GeckoDriver的位置。

保存.bash_profile并强制执行。这会立即加载值,而不必重新启动。您可以执行以下命令:

源~ / . bash_profile

就是这样。你完蛋了!现在可以运行Python脚本了。