简体   繁体   中英

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

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?

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)

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 ie 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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM