当前位置: 首页 > 知识库问答 >
问题:

使用Python-Geckodriver可执行文件的Selenium需要在PATH中

丰飞龙
2023-03-14

我是编程新手,大约两个月前开始使用Python,现在正在复习Sweigart的“用Python文本自动处理无聊的东西”。我正在使用IDLE并且已经安装了Selenium模块和Firefox浏览器。

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.

共有1个答案

裴育
2023-03-14

WebDriverException:消息:“GeckoDriver”可执行文件需要在路径中。

首先,您需要从这里下载最新的可执行geckodriver来使用Selenium运行最新的Firefox

实际上,Selenium客户端绑定试图从systemPath定位GeckoDriver可执行文件。您将需要将包含可执行文件的目录添加到系统路径中。

  export PATH=$PATH:/path/to/directory/of/executable/downloaded/in/previous/step

在Windows上,您需要更新Path系统变量,手动或命令行将完整目录路径添加到可执行geckodriver中**(在将可执行geckodriver添加到系统路径中生效后,不要忘记重新启动系统)**。其原理与UNIX上的原理相同。

现在,您可以运行您的代码,如下所示:-

from selenium import webdriver

browser = webdriver.Firefox()

selenium.common.exceptions.WebDriverException:消息:需要浏览器二进制位置,但在默认位置找不到二进制,未提供'moz:firefoxoptions.binary'功能,命令行上未设置二进制标志

该异常清楚地说明您已经在其他位置安装了Firefox,而Selenium正在尝试查找Firefox并从默认位置启动,但它找不到。您需要明确提供Firefox安装的二进制位置来启动Firefox,如下所示:-

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

binary = FirefoxBinary('path/to/installed firefox binary')
browser = webdriver.Firefox(firefox_binary=binary)

https://github.com/mozilla/geckodriver/releases

对于Windows:

https://github.com/mozilla/geckodriver/releases

对我来说,我的路径路径是:

C:\Users\MYUSERNAME\AppData\Local\Programs\Python\Python39
 类似资料: