How to Install and setup Selenium with Firefox on Ubuntu

  • Post last modified:April 27, 2021
  • Reading time:4 mins read

Selenium is an automated web testing framework. Using this, we can automate the browser functioning for testing any web application.

Using selenium, you can run predefined code to navigate multiple pages and test applications with predefined rules.

In this guide will see how to install and set up selenium with Firefox on Ubuntu and Other Debian-based systems.

You need to follow this guide step by step and make sure not to skip any step.

Basic Requirement

We will use a python program to automate tests. Before this, make sure that you have installed the following Package.

  • Python3 installed on your Ubuntu/Debian rig.
  • PIP3 installed on your Ubuntu/Debian rig.
  • Mozilla Firefox installed on your Ubuntu/Debian system.

Now we will start the installation process.

Step 1: Create Python3 Virtual Environment

First of all we install virtualenv using pip3 command type the following command in terminal.

$ sudo pip3 install virtualenv
Install selenium firefox ubuntu  create virtual environment
Install virtualenv

A benefit of installing virtualenv will create isolated python3 environment, which will ensure that you don’t wreck your old programs.

So whatever module you import for this current project will save on current project directory not on globally.

After virtualenv download you need to create project directory at any location.To create directory use

$ mkdir -p selenium_firefox/drivers

Go to created folder directory selenium_firefox using cd command.

$ cd selenium_firefox

Now we will setup or create Python Virtual environment in the current project directory using the below command:

Command Syntax

$ virtualenv .virtualenvirnomentname

In mycase

$ virtualenv .demo_environment
Create Virtual Environment
Create Virtual Environment

After completing above step now we want to activate the Python virtual environment to activate pass command:

Command Syntax

$ source .virtualenvironmentname/bin/activate

In mycase

$ source .demo_environment/bin/activate
Activate virtual environment
Activate virtual environment

Read this :30 Basic commands which every Linux user should know

Step 2: How to Install Selenium Python Library

It is pretty simple to install selenium on Python Because selenium is available on the Python PyPI repository So, you need to leverage the pip command:

$ pip3 install selenium
Install Selenium using pip3
Install Selenium using pip3

Selenium is installed successfully, and Now we will install gecko driver for firefox.

Step 3: How to Install Gecko Driver for firebox

A selenium require gecko driver to be installed on your Ubuntu Machine if you want to test on Firebox web browser.

To download gecko driver visit their official github page and download the latest version. At present latest version is 0.28.0,0 version can vary when you read this guide.

On github page you need scroll down and download tar.gz file according to your system architecture.

Download geckodriver from github
Download geckodriver from github

I think so your system architecture is too 64-bit and I’m downloading 64-bit geckodriver-v0.28.0-linux64.tar.gz.

Now open the terminal and extract the downloaded file and move to drivers directory using command:

$ tar xvf ~/Downloads/geckodriver-v0.28.0-linux64.tar.gz -C drivers/
Extract tar file to driver directory
Extract tar file to driver directory

To check file is extracted or not

$ ls drivers/
To check extract file
To check extract file

OR

If you want to use wget command to download gecko driver then pass the command in to the drivers folder of current working project.

$ wget https://github.com/mozilla/geckodriver/releases/download/v0.28.0/geckodriver-v0.28.0-linux64.tar.gz

For latest version go to the official page and scroll down then right click on selected file and copy the link and pass on terminal with wget command.

To extract downloaded and remove geckodriver file pass command

$ tar xvf geckodriver-v0.28.0-linux64.tar && rm geckodriver-v0.28.0-linux64.tar 
Extract tar file
Extract tar file

Step 4: Write basic code in python to use Selenium

All the installation process is completed now we will show you how to make selenium python script.

To write code you can use command line editor like VIM,nano ,emac ,or any GUI editor or IDLE.

Selenium code for python
Selenium code for python

You can write the above script or simply do-copy paste.

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
browser = webdriver.Firefox(executable_path="./drivers/geckodriver")
browser.get('http://trendoceans.com/blog')
print('Title: %s' % browser.title)
time.sleep(10)
browser.quit()

Make sure to save file in the current project working directory. After that we will run command using:

$ python3 seleniumdemo.py

This will command will open firexbox browser and go to trendoceans.com/blog and it will fetch the title name, after the 10 second page will get close automatically and you will get the output on terminal.

Site
Automatically site launch in firebox

This is the output you will receive on terminal.

Output in terminal
Output in terminal

Wrap up

This is the pretty and simple way to install selenium on Ubuntu machine.I hope so you able to install If you stuck somewhere while following guide,feel free to comment us.

Like what you read? Please share it with others.
  • 1
    Share

This Post Has 2 Comments

    1. Gagan Mishra

      Glad to know you like it.

Leave a Reply