Google Colaboratory for geckodriver 中的 executable_path 是什么?

What is the executable_path in Google Colaboratory for geckodriver?

我想在 Google Colaboratory with Selenium Python 包中使用 geckodriver。这是我尝试过的(我不是 Ubuntu 方面的专家)

!pip install selenium
!apt-get update 
!apt install firefox-geckodriver
from selenium.webdriver.firefox.options import Options as FirefoxOptions

firefox_options = FirefoxOptions()
firefox_options.add_argument("--headless")
driver = webdriver.Firefox(executable_path=r'/usr/bin/firefox', options=firefox_options)

这里r'/usr/bin/firefox是错误的。我很困惑。有什么解决办法?感谢任何帮助。

executable_path

executable_path 是参数,用户可以通过该参数传递 GeckoDriver 二进制文件的 绝对路径 覆盖 用于 Firefox 47.0.1 及更高版本的 GeckoDriver 二进制文件的系统路径

例子

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

options = Options()
options.add_argument("start-maximized")
options.add_argument("--headless")
driver = webdriver.Firefox(options=options, executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
driver.get("http://google.com/")

但是在您的代码试验中,您通过了 Firefox 二进制文件的 绝对路径 而不是 GeckoDriver 二进制。如果您的用例要传递 Firefox 二进制文件的 绝对路径,您可以使用以下代码行:

from selenium import webdriver

binary = r'C:\Program Files\Mozilla Firefox\firefox.exe'
options = webdriver.FirefoxOptions()
options.binary = binary
options.add_argument("start-maximized")
options.add_argument("--headless")
browser = webdriver.Firefox(firefox_options=options, executable_path="C:\Utility\BrowserDrivers\geckodriver.exe")
browser.get('http://google.com/')

Google-Colaboratory

中的 GeckoDriver

您需要安装geckodriver、firefox和selenium,并将路径添加到您系统中的路径变量或复制到bin目录中,您可以使用以下解决方案:

# install firefox, geckodriver, and selenium
!apt-get update
!pip install selenium
!apt install firefox-geckodriver
!cp /usr/lib/geckodriver /usr/bin
!cp /usr/lib/firefox /usr/bin

from selenium import webdriver

binary = '/usr/bin/firefox'
options = webdriver.FirefoxOptions()
options.binary = binary
options.add_argument('start-maximized')
options.add_argument('--headless')
browser = webdriver.Firefox(firefox_options=options, executable_path='/usr/bin/geckodriver')
browser.get('http://google.com/')

更新 1

根据您在评论中提到的错误,当您使用 ipython 时,选项应在单引号内传递,如 start-maximized--headless。此外,在指定 executable_path 时, raw string literals markerstring 之间不应有任何 space 字符

You can find a relevant discussion in


更新 2

对于GeckoDriverSeleniumFirefox Browser兼容性图表你可以找到相关的讨论在