我是编程新手,大约两个月前开始学习Python,现在正在复习斯维加特的Automate the Boring Stuff with Python课文.我正在使用IDLE,并且已经安装了Selenium模块和Firefox浏览器.

每当我try 运行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.WebDriver例外情况: Message: 'geckodriver' executable needs to be in PATH.

我想我需要将路径设置为geckodriver,但是我不确定如何设置,那么我该如何做呢?

推荐答案

selenium.常见的例外.WebDriverException:消息:"geckodriver"可执行文件需要位于路径中.

First of all you will need to download latest executable geckodriver from here to run latest Firefox using Selenium

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

  • 在Unix系统上,如果您使用的是与Bash兼容的shell,则可以执行以下操作将其附加到系统的搜索路径:

      export PATH=$PATH:/path/to/directory/of/executable/downloaded/in/previous/step
    
  • 在Windows上,您需要更新Path system variable to add the full directory path to the executable geckodriver manuallycommand line**(在将可执行geckodriver添加到系统路径后,不要忘记重新启动系统以生效)**.其原理与在Unix上相同.

现在,您可以按如下方式运行代码:-

from selenium import webdriver

browser = webdriver.Firefox()

selenium.常见的例外.WebDriverException:消息:预期的浏览器二进制位置,但在默认位置找不到二进制,没有"moz:firefoxOptions".提供了"二进制"功能,并且在命令行上没有设置二进制标志

例外情况清楚地表明,当Selenium试图找到Firefox并从默认位置启动时,您已经在其他位置安装了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:

从GitHub下载该文件,将其解压缩并粘贴到Python文件中.这对我很管用.

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

对我来说,我的道路是:

C:\Users\MYUSERNAME\AppData\Local\Programs\Python\Python39

Python相关问答推荐

线性模型PanelOLS和statmodels OLS之间的区别

重新匹配{ }中包含的文本,其中文本可能包含{{var}

难以在Manim中正确定位对象

为什么符号没有按顺序添加?

如何列举Pandigital Prime Set

Python键入协议默认值

使用Python更新字典中的值

如何禁用FastAPI应用程序的Swagger UI autodoc中的application/json?

如何防止Pandas将索引标为周期?

python—telegraph—bot send_voice发送空文件

在Python中使用yaml渲染(多行字符串)

在Python中从嵌套的for循环中获取插值

如何获得3D点的平移和旋转,给定的点已经旋转?

如何使用Azure Function将xlsb转换为xlsx?

Js的查询结果可以在PC Chrome上显示,但不能在Android Chrome、OPERA和EDGE上显示,而两者都可以在Firefox上运行

如何在PythonPandas 中对同一个浮动列进行逐行划分?

将字节序列解码为Unicode字符串

函数()参数';代码';必须是代码而不是字符串

Numpy`astype(Int)`给出`np.int64`而不是`int`-怎么办?

为什么在安装了64位Python的64位Windows 10上以32位运行?