首页 > 解决方案 > 'geckodriver' 可执行文件需要通过 Selenium 使用 GeckoDriver 和 Firefox 在 PATH 中

问题描述

我非常熟悉使用 chromedriver for selenium,我现在尝试使用 geckdriver 但由于某种原因我不断收到错误'geckodriver' executable needs to be in PATH.

我按照Selenium 中的步骤使用 Python - Geckodriver 可执行文件需要在 PATH 中

但是这些方法似乎都不起作用,我缺少什么吗?

这是我的代码

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


binary = FirefoxBinary("C:\\Users\\ojadi\\Downloads\\geckodriver-v0.28.0-win64\\geckodriver.exe")
browser = webdriver.Firefox(firefox_binary=binary)

标签: pythonseleniumfirefoxgeckodriverselenium-firefoxdriver

解决方案


您可以在系统中的任何位置下载和存储GeckoDriver可执行文件,您需要firefox通过属性传递二进制文件的绝对路径,binary_location如下所示:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.binary_location = r'C:\Program Files\Mozilla Firefox\firefox.exe'
driver = webdriver.Firefox(firefox_options=options, executable_path=r'C:\Users\\ojadi\Downloads\geckodriver-v0.28.0-win64\geckodriver.exe')
driver.get('http://google.com/')

推荐阅读