Selenium is an open-source tool or framework that helps testers or developers to run automated tests on web-browser. You can run selenium on Windows,macOS, and Linux.
Selenium support several popular languages for testing like C#, Groovy, Perl, Java, PHP, Python, Ruby, and Scala.
Through this article, you will see how to set up selenium on Chrome Browser with chrome driver and sample python script to verify our installation.
Table of Contents
In this guide, you will use a python program to automate tests, so to move ahead, you should make sure the following applications and dependencies are preinstalled.
Once the prerequisite is fulfilled, you can move to Create Virtual Environment.
Step 1: Create Python3 Virtual Environment
This step is not necessary to follow. But still, I suggest you to create a python3 virtual environment, which will ensure that your old python programs do not interfere with the new project.
But how virtualenv will keep my old programs safe to run without error? Once you install and activate virtualenv, it will create a separate or isolated environment where you can Install different python libraries or modules without touching your old available libraries.
Once you were satisfied with the above explanation, you are bound to Install virtualenv using the below command. Before that, make sure you have installed pip3.
$ sudo pip3 install virtualenv
The next step is to create a project directory where you will install selenium. To create a directory, pass the following commands:
$ mkdir -p selenium_chrome/drivers
This will create multiple directories in the current folder. After that, change your current directory to selenium_chrome
$ cd selenium_chrome
This is a final step where you will create a python virtual environment in the above directory using the virtualenv command:
$ virtualenv .virtualenvironmentname
And, of course, change .virtualenvironmentname with a desirable name.
Note:- While performing step’s make sure to use bash editor,to avoid unforeseen error.
One last step is to activate the Virtual environment for that pass the below command and don’t forget to use source otherwise you may get the permission error:
$ source .virtualenvironmentname/bin/activate
Step 2: How to Install Selenium Python Library
Once you complete the above procedures, move ahead to Install Selenium, and It’s quite simple to Install Selenium using the PIP command:
$ pip3 install selenium
Intentinonally we have trimmed the output to avoid confusion, In your case download package list can be more or lesser depend upon your previous python usage.
After that you were able to install selenium a virtual environment, and the next step is to download & install the chrome driver for the Chrome browser.
Step 3: How to Install Chrome Driver for Chrome Browser
Now you may undestand why selenuim need chrome driver as a companion.
Steps to Install a chrome driver in Ubuntu as follows: First go to the official page .
Once the page gets loaded, you will see a different release or version of the chrome driver. To Download chrome browser you should now which version of Chrome Browser your are currently using. We can check the chrome browser as follows:
- Step 1. Click on the three dots which is below the close button.
- Step 2. Next step is to click on Settings.
Step 3. Go to About Chrome tab and check the chrome version. In my case chrome version is 96.0.46.
Once you are confirmed about a chrome version, let’s download the chrome driver. In my case, I’ll Download ChromeDriver 96.0.4664.45, and you may download the chrome driver according to the respective Chrome Browser version.
A vesion of chrome browser which I’m having in my system is not the latest as per the above image, so the download older version of chrome driver click on Downloads.
After that it will list the version, choose the respective version according to browser version.
When you click on a Downloads, It will redirect to the Index page to download the chrome driver for the Linux-based system.
In this guide, we will use
wget to download the file. To download, make a right-click on chromedriver_linux64.zip and copy the link.
Now go to the terminal screen and copy-paste the below command, and replace it with the right chrome driver version link as per your chrome browser version.
$ wget https://chromedriver.storage.googleapis.com/96.0.4664.45/chromedriver_linux64.zip
After this extract, downloaded zip file to driver directory using
unzip command with a flag -d parameter.
$ unzip chromedriver_linux64.zip -d drivers/
After that, pass the ls command to verify whether the file is extracted as per our intention or not.
$ ls -l drivers/
Step 4: Write basic code in python to use Selenium
Once we completed the above process, we will move to create a selenium script in python language.
You can use any command-line editor such as vim, nano, emacs, or any other GUI editors to create a script.
Before writing the script, first, understand what our script will do? Basically, this script will open a chrome browser automatically, and it will go to a webpage called “trendoceans” and after that script will fetch the Webpage Title, and once it fetches after 10 seconds browser will close automatically, and the webpage title will display on the terminal screen.
I believe you are in the virtual environment; pass the below command to create a new python file:
$ nano seleniumscript.py
After that, copy-paste the below code, and to save, press Ctrl +O for exit use Ctrl + X.
import time from selenium import webdriver from selenium.webdriver.common.keys import Keys browser = webdriver.Chrome(executable_path="./drivers/chromedriver") browser.get('https://trendoceans.com') print('Title: %s' % browser.title) time.sleep(10) browser.quit()
Step 5: Run Python Script
You are almost done with the setup. Now it’s time to check whether everything runs as per our plan or not. To check type the below command
$ python3 seleniumscript.py
Once you run the above script it will load the web page & after getting the web page title, A browser will get close automatically after 10 seconds.
The very next moment webpage title will be displayed on your terminal window.
That’s all to Download and Setup Selenium on Ubuntu or any Debian-based distributions machines, personally I have tested Installation on Debian 11 & Ubuntu 20.04, and I belive it work in all major Linux Distrubutions.
If you are stuck somewhere, please feel free to comment down and If you like the article, or somewhere I missed something, please let me know to make this article more amazing.
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.”