Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set up a Selenium Python environment for Firefox

How can I set up a Selenium Python environment for Firefox?

I am using Firefox 50, Selenium 3, Python 3.5. I tried with many things binary and copying the geckodriver in the environment variable PATH, etc.

like image 207
jaibalaji Avatar asked Feb 13 '17 13:02

jaibalaji


People also ask

How do I run Selenium Python in Firefox?

To make Firefox work with Python selenium, you need to install the geckodriver. The geckodriver driver will start the real firefox browser and supports Javascript. Take a look at the selenium firefox code. First import the webdriver, then make it start firefox.

How do I use Selenium for Firefox?

Step 1: Navigate to the official Selenium website. Under third-party drivers, one will find all the drivers. Just click on the Mozilla GeckoDriver documentation as shown below. Now, it will navigate to the GeckoDriver downloads link, where one can download the suitable driver based on the OS as it is platform agnostic.

What is the name of class to launch Firefox using Python?

GeckoDriver is what is between Selenium and the Firefox browser. It lets us control the Firefox web browser from Python code. All web browser commands go through the GeckoDriver, the GeckoDriver in turn makes your browser do what you want. The GeckoDriver is a different executable on every operating system.


2 Answers

The testing machine should have Selenium v. 3.0.2, Firefox v. 51.0.1 (latest version) and geckodriver v. 0.14. If you are using Linux, please do the following steps:

[Look up the latest release on GitHub (or from the API) and replace the wget link with that. Downloading and installing an outdating release may result in "buggy" behaviour.]

apt-get update
apt-get install firefox
pip3 install selenium==3.0.2
wget https://github.com/mozilla/geckodriver/releases/download/vX.XX.0/geckodriver-vX.XX.0-linuxXX.tar.gz -O /tmp/geckodriver.tar.gz \
  && tar -C /opt -xzf /tmp/geckodriver.tar.gz \
  && chmod 755 /opt/geckodriver \
  && ln -fs /opt/geckodriver /usr/bin/geckodriver \
  && ln -fs /opt/geckodriver /usr/local/bin/geckodriver

Select the version for your operating system from the available compressed pre-built binaries.

Here is an example to run:

from selenium import webdriver
driver = webdriver.Firefox()
driver.get('http://google.com')
print(driver.title)
driver.quit()
like image 57
IslamTaha Avatar answered Oct 06 '22 08:10

IslamTaha


  1. In Windows install Python from: https://www.python.org/downloads/

  2. Then run pip install from the command line: pip install selenium

  3. Download the Gecko/Chrome/Internet Explorer driver and add the driver.exe path to the PATH environment variable. So the need to set up the path while running Selenium driver.Firefox() / driver.Chrome() method.

like image 32
jaibalaji Avatar answered Oct 06 '22 09:10

jaibalaji