Google Colaboratory中geckodriver的execute_path是什么? [英] What is the executable_path in Google Colaboratory for geckodriver?

查看:93
本文介绍了Google Colaboratory中geckodriver的execute_path是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Google Colaboratory的Selenium Python软件包中使用geckodriver.这是我尝试过的(我不是Ubuntu专家)

I want to use geckodriver in Google Colaboratory with Selenium Python package. Here is what I tried (I'm not an expert in 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是错误的.我很困惑.有什么解决方案?感谢您的帮助.

Here r'/usr/bin/firefox is wrong. I'm confused. What can be the solution? Any help is appreciated.

推荐答案

executable_path

executable_path

executable_path is the parameter through which users can pass the absolute path of the GeckoDriver binary overriding the system path of GeckoDriver binary to be used for Firefox 47.0.1 and greater.

示例

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 二进制文件的绝对路径,则可以使用以下代码行:


But in your code trials you have passed the absolute path of the Firefox binary instead of the GeckoDriver binary. If your usecase is to pass the absolute path of the Firefox binary as well you can use the following line of code:

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协作平台中的GeckoDriver

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


GeckoDriver in Google-Colaboratory

You need to install the geckodriver, firefox and selenium and add the path to your path variable within your system or copy within the bin directory and you can use the following solution:

# 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 marker string


Update 1

As per the error you mentioned within the comments, as you are using ipython the options should be passed within single quotes as start-maximized and --headless. Additionally, while specifying executable_path there shouldn't be any space character between the raw string literals marker and the string

您可以在 SyntaxError中找到相关的讨论: ipython中的execute_path语法无效


更新2

对于 GeckoDriver Selenium Firefox浏览器兼容性图表,您可以在


Update 2

For GeckoDriver, Selenium and Firefox Browser compatibility chart you can find a relevant discussion in WebDriverException: Message: invalid argument: can't kill an exited process with GeckoDriver, Selenium and Python on RaspberryPi3

这篇关于Google Colaboratory中geckodriver的execute_path是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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