[英]What is the executable_path in Google Colaboratory for geckodriver?
I want to use geckodriver in Google Colaboratory with Selenium Python package.我想在带有 Selenium Python 包的 Google Colaboratory 中使用 geckodriver。 Here is what I tried (I'm not an expert in Ubuntu)
这是我尝试过的(我不是 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)
Here r'/usr/bin/firefox
is wrong.这里
r'/usr/bin/firefox
是错误的。 I'm confused.我糊涂了。 What can be the solution?
解决办法是什么? Any help is appreciated.
任何帮助表示赞赏。
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. executable_path是通过它用户可以通过GeckoDriver的绝对路径二进制重写GeckoDriver二进制的系统路径用于火狐47.0.1和更大的参数。
Example例子
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/")
But in your code trials you have passed the absolute path of the Firefox binary instead of the GeckoDriver binary.但是在您的代码试验中,您传递了Firefox二进制文件的绝对路径,而不是GeckoDriver二进制文件。 If your usecase is to pass the absolute path of the Firefox binary as well you can use the following line of code:
如果您的用例也是传递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/')
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:您需要安装 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/')
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
.根据您在评论中提到的错误,当您使用
ipython
,选项应在单引号内作为start-maximized
和--headless
。 Additionally, while specifying executable_path
there shouldn't be any space character between the raw string literals marker
and the string
此外,在指定
executable_path
, raw string literals marker
和string
之间不应有任何空格字符
You can find a relevant discussion in SyntaxError: invalid syntax with executable_path in ipython
您可以在SyntaxError: invalid syntax with executable_path in ipython 中找到相关讨论
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对于GeckoDriver 、 Selenium和Firefox 浏览器兼容性图表,您可以在WebDriverException: Message: invalid argument: can't kill an exited process with GeckoDriver, Selenium 和 Python on RaspberryPi3 中找到相关讨论
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.