Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the executable_path in Google Colaboratory for geckodriver?

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.

like image 722
vesszabo Avatar asked Oct 15 '22 12:10

vesszabo


1 Answers

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

like image 129
undetected Selenium Avatar answered Oct 20 '22 11:10

undetected Selenium