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

4

6

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?

Hesitation answered 10/9, 2019 at 11:57 Comment(3)
Is that downloaded driver located in "/home/andrea/geckodriver" from this page? Is it an executable (chmod +x)?Neysa
@Neysa yes it even opens mozilla firefox but then it closes and the console displayes the error message I wrote aboveHesitation
Have you tried with this brand new driver github.com/mozilla/geckodriver/releases?Neysa
F
7

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)
Fisher answered 30/1, 2020 at 10:45 Comment(0)
A
3

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

Aylmer answered 10/9, 2019 at 15:7 Comment(0)
C
1

Step1: Install Selenium Type in Terminal (in Ubuntu)

$ pip install selenium

Step2: Download Geckodriver

https://pypi.org/project/geckodriver-autoinstaller/

Step3: Inside your python code file

from selenium import webdriver                  # Import selenium into your program
from selenium.webdriver.common.keys import Keys # Import keys of selenium web driver
import geckodriver_autoinstaller                # import Geckodriver into your program
geckodriver_autoinstaller.install()             # Get the latest version every day on 1st excution of your program
driver = webdriver.Firefox()                    # initiate the firefox driver 
driver.get("Your Website URL")
Cermet answered 1/3, 2023 at 7:25 Comment(0)
E
0

If you want to use Firefox with Selenium, you need to import e Firefox Profile. You can use your own Profile through the following steps :

  1. Locate the Firefox Profile directory
  2. You have to specify the absolute path of the Firefox Profile directory when you initiate the webdriver.

    from selenium import webdriver
    profile = webdriver.FirefoxProfile(*path to your profile*)
    driver = webdriver.Firefox(firefox_profile=profile)
    
Elaterite answered 10/9, 2019 at 12:9 Comment(1)
found it, but this didnt work! it gives me the same errorHesitation

© 2022 - 2024 — McMap. All rights reserved.