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

2024-04-26 08:04:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我是编程新手,大约两个月前开始使用Python,现在我将介绍Sweigart的使用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设置路径,但不确定如何设置路径,所以有人能告诉我怎么做吗?


Tags: inpyselflibpackagesseleniumserviceline
3条回答

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

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

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

    export PATH=$PATH:/path/to/directory/of/executable/downloaded/in/previous/step
    
  • 在Windows上,您需要更新Path系统变量,以将完整目录路径添加到可执行geckodriver中manuallycommand line(在将可执行geckodriver添加到系统路径后不要忘记重新启动系统以使其生效)。原理与Unix相同。

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

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

异常清楚地表明,当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)

这帮我解决了问题。

from selenium import webdriver
driver = webdriver.Firefox(executable_path=r'your\path\geckodriver.exe')
driver.get('http://inventwithpython.com')

这个步骤在ubuntu firefox 50上为我解决了。

  1. 下载geckodriver

  2. 在/usr/local/bin中复制geckodriver

您不需要添加

firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
firefox_capabilities['binary'] = '/usr/bin/firefox'
browser = webdriver.Firefox(capabilities=firefox_capabilities)

相关问题 更多 >