What is the executable_path in Google Colaboratory for geckodriver?
Asked Answered
I

1

8

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)

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

Immobility answered 16/8, 2019 at 17:16 Comment(1)
@orde Not duplicate. My question is about geckodriver, and in Google Colaboratory. In my PC chromedriver, geckodriver work well. In Google Colaboratory I could successfully instal chromedriver and works well. However webdriver.Firefox is a little bit different. In "principle" I know what to do, but I was unsuccessful to find the necessary details.Immobility
V
5

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.

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. 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/')

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/')

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

You can find a relevant discussion in SyntaxError: invalid syntax with executable_path in ipython


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

Vachill answered 19/8, 2019 at 9:32 Comment(11)
@Immobility Checkout the answer update and let me know the status.Vachill
Your solution is correct, firefox, geckodriver are in /usr/bin. However ----> 6 browser = webdriver.Firefox(options=options, executable_path='/usr/bin/geckodriver') WebDriverException: Message: invalid argument: can't kill an exited process I searched stackoverflow and #52535158 says this is a version problem. I don't know which version should I choose. I tried !apt install -upgrade firefox-geckodriver , but not helped. Please, a little bit more help.Immobility
I deleted my previous comment because when I finished the editing and wanted to add, I used more then 5 minutes and presented only a part of my comment. Sorry for the inconvenience.Immobility
@Immobility Updated the linked answer with the compatibility chart. Let me know if any queries.Vachill
I tried what you recommended !apt install firefox=62.0.2, !apt install geckodriver=0.22.0, but E: Version '62.0.2' for 'firefox' was not found, E: Unable to locate package geckodriver. I also tried !apt install firefox-geckodriver=68.0.1 (pkgs.org/download/firefox-geckodriver), but E: Version '68.0.1' for 'firefox-geckodriver' was not found. I'm absolutely confused about versions.Immobility
@Immobility IMO, you have to raise a new question with the new error invalid argument: can't kill an exited process including the OS, Binary versions and error stack trace.Vachill
I asked here #57581103Immobility
I've followed the way to set up in colab but not working I asked here #64731018Vacancy
!cp /usr/lib/geckodriver /usr/bin results in error, there's no such file, the geckodriver is stored in /usr/bin/geckodriver. !cp /usr/lib/firefox /usr/bin doesn't work too, /usr/lib/firefox is a directory, not a file. Overall, the code fails with the exception "Process unexpectedly closed with status signal". Please update your answer.Kilgore
Hey @Kilgore I had the same issue and someone actually came up with a solution https://mcmap.net/q/376720/-webdriverexception-message-process-unexpectedly-closed-with-status-signal-how-to-run-selenium-firefox-geckodriver-in-google-colaboratoryDachia
I tried this on google colab but got "WebDriverException: Message: Process unexpectedly closed with status signal"Hube

© 2022 - 2024 — McMap. All rights reserved.