【问题标题】:Selenium using Python - Geckodriver executable needs to be in PATHSelenium 使用 Python - Geckodriver 可执行文件需要在 PATH 中
【发布时间】:2023-04-03 01:30:01
【问题描述】:

我是编程新手,大约两个月前开始使用 Python,现在正在阅读 Sweigart 的 Automate the Boring Stuff with Python 文本。我正在使用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

During handling of the above exception, another exception occurred:

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 设置路径,但我不知道该怎么做,我该怎么做呢?

【问题讨论】:

  • 请看我对类似问题的回答here
  • 我将 geckodriver.exe 放在 Python/Python35 目录中,因此它具有相同的路径,但我遇到了更多问题。
  • 在 Mac 上:brew install geckodriver
  • 我发现通过 Chrome 浏览器运行它比在 Firefox 上运行它要快一点,您只需为此下载chromedriver
  • 注意:Testcafe 最近开源了。它不需要任何浏览器插件,它们是内置的。我想使用 Selenium,但这看起来是一个有趣的选择。

标签:
python
selenium
firefox
selenium-firefoxdriver
geckodriver