Selenium 使用 Python - Geckodriver 可执行文件需要在 PATH 中 [英] Selenium using Python - Geckodriver executable needs to be in PATH

查看:62
本文介绍了Selenium 使用 Python - Geckodriver 可执行文件需要在 PATH 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程新手,大约两个月前开始使用 Python,现在正在阅读 Sweigart 的 Automate the Boring Stuff with Python 文本.我正在使用 IDLE 并且已经安装了 Selenium 模块和 Firefox 浏览器.

每当我尝试运行 webdriver 函数时,我都会得到:

从 selenium 导入 webdriver浏览器 = webdriver.Firefox()

例外:

Exception被忽略:<selenium.webdriver.firefox.service.Service对象的绑定方法Service.__del__在0x00000249C0DA1080>>回溯(最近一次通话最后):文件C:PythonPython35libsite-packagesseleniumwebdrivercommonservice.py",第 163 行,在 __del__自我停止()文件C:PythonPython35libsite-packagesseleniumwebdrivercommonservice.py",第 135 行,停止如果 self.process 为无:AttributeError:服务"对象没有属性进程"异常被忽略:<selenium.webdriver.firefox.service.Service 对象的绑定方法 Service.__del__ 位于 0x00000249C0E08128>>回溯(最近一次通话最后):文件C:PythonPython35libsite-packagesseleniumwebdrivercommonservice.py",第 163 行,在 __del__自我停止()文件C:PythonPython35libsite-packagesseleniumwebdrivercommonservice.py",第 135 行,停止如果 self.process 为无:AttributeError:服务"对象没有属性进程"回溯(最近一次通话最后):文件C:PythonPython35libsite-packagesseleniumwebdrivercommonservice.py",第 64 行,开始标准输出=self.log_file,标准错误=self.log_file)文件C:PythonPython35libsubprocess.py",第 947 行,在 __init__恢复信号,开始新会话)_execute_child 中的文件C:PythonPython35libsubprocess.py",第 1224 行启动信息)FileNotFoundError: [WinError 2] 系统找不到指定的文件在处理上述异常的过程中,又出现了一个异常:回溯(最近一次通话最后):文件<pyshell#11>",第 1 行,在 <module>浏览器 = webdriver.Firefox()文件C:PythonPython35libsite-packagesseleniumwebdriverfirefoxwebdriver.py",第 135 行,在 __init__self.service.start()文件C:PythonPython35libsite-packagesseleniumwebdrivercommonservice.py",第 71 行,开始os.path.basename(self.path), self.start_error_message)selenium.common.exceptions.WebDriverException:消息:geckodriver"可执行文件需要在 PATH 中.

我想我需要为 geckodriver 设置路径,但我不知道该怎么做,我该怎么做呢?

解决方案

selenium.common.exceptions.WebDriverException:消息:geckodriver"可执行文件需要在 PATH 中.

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

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

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

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

  • 在 Windows 上,您需要更新 Path 系统变量以将完整目录路径添加到可执行 geckodriver 手动命令行**(不要将可执行的 geckodriver 添加到系统 PATH 后忘记重新启动系统)**.原理和Unix一样.

现在你可以像下面一样运行你的代码了:-

从 selenium 导入 webdriver浏览器 = webdriver.Firefox()

<块引用>

selenium.common.exceptions.WebDriverException: 消息:预期的浏览器二进制位置,但无法在默认位置找到二进制文件,没有提供moz:firefoxOptions.binary"功能,命令行上没有设置二进制标志

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

从 selenium 导入 webdriver从 selenium.webdriver.firefox.firefox_binary 导入 FirefoxBinarybinary = FirefoxBinary('path/to/installed firefox binary')浏览器 = webdriver.Firefox(firefox_binary=binary)

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

对于 Windows:

从 GitHub 下载文件,解压并粘贴到 Python 文件中.它对我有用.

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

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

C:UsersMYUSERNAMEAppDataLocalProgramsPythonPython39

I'm new to programming and started with Python about two months ago and am going over Sweigart's Automate the Boring Stuff with Python text. I'm using IDLE and already installed the Selenium module and the Firefox browser.

Whenever I tried to run the webdriver function, I get this:

from selenium import webdriver
browser = webdriver.Firefox()

Exception:

Exception ignored in: <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x00000249C0DA1080>>
Traceback (most recent call last):
  File "C:PythonPython35libsite-packagesseleniumwebdrivercommonservice.py", line 163, in __del__
    self.stop()
  File "C:PythonPython35libsite-packagesseleniumwebdrivercommonservice.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:PythonPython35libsite-packagesseleniumwebdrivercommonservice.py", line 163, in __del__
    self.stop()
  File "C:PythonPython35libsite-packagesseleniumwebdrivercommonservice.py", line 135, in stop
    if self.process is None:
AttributeError: 'Service' object has no attribute 'process'
Traceback (most recent call last):
  File "C:PythonPython35libsite-packagesseleniumwebdrivercommonservice.py", line 64, in start
    stdout=self.log_file, stderr=self.log_file)
  File "C:PythonPython35libsubprocess.py", line 947, in __init__
    restore_signals, start_new_session)
  File "C:PythonPython35libsubprocess.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:PythonPython35libsite-packagesseleniumwebdriverfirefoxwebdriver.py", line 135, in __init__
    self.service.start()
  File "C:PythonPython35libsite-packagesseleniumwebdrivercommonservice.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.

I think I need to set the path for geckodriver, but I am not sure how, so how would I do this?

解决方案

selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.

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

Actually, the Selenium client bindings tries to locate the geckodriver executable from the system PATH. You will need to add the directory containing the executable to the system path.

  • On Unix systems you can do the following to append it to your system’s search path, if you’re using a Bash-compatible shell:

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

  • On Windows you will need to update the Path system variable to add the full directory path to the executable geckodriver manually or command line** (don't forget to restart your system after adding executable geckodriver into system PATH to take effect)**. The principle is the same as on Unix.

Now you can run your code same as you're doing as below :-

from selenium import webdriver

browser = webdriver.Firefox()

selenium.common.exceptions.WebDriverException: Message: Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided, and no binary flag set on the command line

The exception clearly states you have installed Firefox some other location while Selenium is trying to find Firefox and launch from the default location, but it couldn't find it. You need to provide explicitly Firefox installed binary location to launch Firefox as below :-

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

For Windows:

Download the file from GitHub, extract it, and paste it in Python file. It worked for me.

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

For me, my path path is:

C:UsersMYUSERNAMEAppDataLocalProgramsPythonPython39

这篇关于Selenium 使用 Python - Geckodriver 可执行文件需要在 PATH 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆