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.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
vesszabo
  • 433
  • 8
  • 13
  • 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. – vesszabo Aug 16 '19 at 19:13

1 Answers1

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

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • @vesszabo Checkout the answer update and let me know the status. – undetected Selenium Aug 20 '19 at 14:51
  • 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 https://stackoverflow.com/questions/52534658/webdriverexception-message-invalid-argument-cant-kill-an-exited-process-with 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. – vesszabo Aug 20 '19 at 14:56
  • 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. – vesszabo Aug 20 '19 at 15:01
  • @vesszabo Updated the [linked answer](https://stackoverflow.com/questions/52534658/webdriverexception-message-invalid-argument-cant-kill-an-exited-process-with/52535654#52535654) with the compatibility chart. Let me know if any queries. – undetected Selenium Aug 20 '19 at 15:04
  • 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 (https://pkgs.org/download/firefox-geckodriver), but E: Version '68.0.1' for 'firefox-geckodriver' was not found. I'm absolutely confused about versions. – vesszabo Aug 20 '19 at 15:52
  • @vesszabo 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. – undetected Selenium Aug 20 '19 at 16:10
  • I asked here https://stackoverflow.com/questions/57580603/invalid-argument-cant-kill-an-exited-process – vesszabo Aug 20 '19 at 19:46
  • I've followed the way to set up in `colab` but not working I asked here https://stackoverflow.com/questions/64730518/how-to-setup-selenium-geckodriver-on-google-colab – Darkknight Nov 08 '20 at 08:58
  • 1
    `!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. – upper Jan 29 '21 at 10:34
  • Hey @upper I had the same issue and someone actually came up with a solution https://stackoverflow.com/a/69896490/15163882 – Mika C. Nov 20 '21 at 04:04
  • 2
    I tried this on google colab but got "WebDriverException: Message: Process unexpectedly closed with status signal" – kawingkelvin Mar 07 '22 at 19:35