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.
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
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:
$ virtualenv .virtualenvirnomentname
$ virtualenv .demo_environment
After completing above step now we want to activate the Python virtual environment to activate pass command:
$ source .virtualenvironmentname/bin/activate
$ source .demo_environment/bin/activate
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
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.
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/
To check file is extracted or not
$ ls drivers/
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
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.
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.
This is the output you will receive on terminal.
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.
A man with a tech effusive, who has explored some of the amazing technology stuff and exploring more, While moving towards, I had a chance to work on Android Development, Linux, AWS, DevOps with several Open source tools.
One of my life mottos “Always be lifelong Students.”