How to properly use selenium with geckodriver and firefox with python on Ubuntu?

10,680

Solution 1

Step1: Install Selenium

Type in Terminal(in Ubuntu) or in Command Prompt(in Windows)

$pip install selenium

Step2: Download Geckodriver

In order to work with Selenium there should be an executable called 'Gecko Driver' installed.

Download Gecko Driver from the following page:

https://github.com/mozilla/geckodriver/releases

Step3: Install Gecko Driver

Latest version for Windows:

https://github.com/mozilla/geckodriver/releases/download/v0.26.0/geckodriver-v0.26.0-win64.zip

Latest version for Ubuntu:

https://github.com/mozilla/geckodriver/releases/download/v0.26.0/geckodriver-v0.26.0-linux64.tar.gz

Setup Gecko Driver For Windows: Extract the zip file and move the geckodiver.exe executable file to any location which is already in Path variable(For Example you can move it to Python path location)

Unless add the path of 'geckodriver.exe' to the Path variable

Setup Gecko Driver For Ubuntu: Open Terminal

Ctrl+Alt+T

move directory to the location where tar file is downloaded Usually it will be in Downloads. so type $ cd Downloads

Unzip the tar file eg:

$sudo tar -xvf filename.tar.gz

In my case it is:

$sudo tar -xvf geckodriver-v0.26.0-linux64.tar.gz

Move the geckodriver executable file to the '/usr/local/bin' location $sudo mv geckodriver /usr/local/bin/

Move the directory to '/usr/local/bin/'

$cd /usr/local/bin/

Now make executable permission for 'geckodriver' executable file

$sudo chmod +x geckodriver

Now type 'geckodriver' in Terminal

geckodriver

If Gecko Driver is not working still then add its path

$export PATH=$PATH:/usr/local/bin/geckodriver

Now it is ready to work with selenium

Sample Code

Some sample codes are here:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import ui

driver = webdriver.Firefox()
driver.get('https://www.google.com/')
page_url=driver.find_elements_by_xpath("//a[@class='content']")
all_title = driver.find_elements_by_class_name("title")
title = [title.text for title in all_title]
print(title)

Solution 2

This error message...

selenium.common.exceptions.WebDriverException: Message: Can't load the profile. 
Possible firefox version mismatch. You must use GeckoDriver instead for Firefox 48+. Profile Dir: /tmp/tmpuigrk9f7 If you specified a log_file in the FirefoxBinary constructor, check it for details.

...implies that there was a mismatch between the GeckoDriver and Firefox version while initiating/spawning a new WebBrowsing Session i.e. Firefox Browser session.

Your main issue is the incompatibility between the version of the binaries you are using as follows:

  • You are using Mozilla Firefox v68.0.2
  • Your Selenium Client version is is unknown to us.
  • Your GeckoDriver version is unknown to us.

However as you are using Mozilla Firefox v68.0.2, using GeckoDriver is mandatory and while you use GeckoDriver you can't set the capability marionette as False.

You can find a detailed discussion in How can Geckodriver/Firefox work without Marionette? (running python selenium 3 against FF 53)


Solution

  • Upgrade Selenium to current levels Version 3.141.59.
  • Upgrade GeckoDriver to current GeckoDriver v0.24.0 level.
  • GeckoDriver is present in the specified location.
  • GeckoDriver is having executable permission for non-root users.
  • Upgrade Firefox version to Firefox v68.0.2 levels.
  • Clean your Project Workspace through your IDE and Rebuild your project with required dependencies only.
  • If your base Web Client version is too old, then uninstall it through Revo Uninstaller and install a recent GA and released version of Web Client.
  • Take a System Reboot.
  • Execute your Test as a non-root user.
  • Always invoke driver.quit() within tearDown(){} method to close & destroy the WebDriver and Web Client instances gracefully.

Outro

GeckoDriver, Selenium and Firefox Browser compatibility chart

supported_platforms_geckodriver_24

Share:
10,680
hispaniccoder
Author by

hispaniccoder

Updated on June 04, 2022

Comments

  • hispaniccoder
    hispaniccoder almost 2 years

    I am trying to use the geckodriver with firefox and selenium on my Ubuntu machine. This is the code I have so far:

    from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
    from selenium.webdriver.firefox.options import Options
    from selenium.webdriver.common.keys import Keys
    from selenium import webdriver
    
    
    #path where browser is installed
    binary = '/usr/bin/firefox'
    options = webdriver.FirefoxOptions()
    options.binary = binary
    options.add_argument('start-maximized')
    options.add_argument('--headless')
    
    
    cap = DesiredCapabilities().FIREFOX
    cap["marionette"] = False
    
    
    path_to_driver = "/home/andrea/geckodriver"
    
    # run firefox webdriver from executable path 
    driver = webdriver.Firefox(firefox_options=options, capabilities=cap, executable_path = path_to_driver)
    #driver = webdriver.Firefox(capabilities=cap, executable_path = path_to_driver)
    
    
    driver.get("https://www.amboss.com/us/account/login")
    
    

    Despite that I am getting the following error:

    selenium.common.exceptions.WebDriverException: Message: Can't load the profile. 
    Possible firefox version mismatch. You must use GeckoDriver instead for Firefox 48+. Profile Dir: /tmp/tmpuigrk9f7 If you specified a log_file in the FirefoxBinary constructor, check it for details.
    

    The firefox version which I work with is: Mozilla Firefox 68.0.2

    Does anyone have any idea as to how I could go about fixing this?

    • ipaleka
      ipaleka over 4 years
      Is that downloaded driver located in "/home/andrea/geckodriver" from this page? Is it an executable (chmod +x)?
    • hispaniccoder
      hispaniccoder over 4 years
      @ipaleka yes it even opens mozilla firefox but then it closes and the console displayes the error message I wrote above
    • ipaleka
      ipaleka over 4 years
      Have you tried with this brand new driver github.com/mozilla/geckodriver/releases?
  • hispaniccoder
    hispaniccoder over 4 years
    found it, but this didnt work! it gives me the same error