How to Install and setup Selenium with Google Chrome on Ubuntu

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

Selenium is an open-source tool or framework that helps tester or developer to run automated tests executed 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, we will see how to set up selenium on Chrome Browser using chrome driver.

Prerequistes

In this guide, we will use a python program to automate test, so to move ahead, we 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. Still, I advise you to create a python3 virtual environment, which will ensure that other python programs don’t get any error while running.

But how virtualenv will protect other available programs to run without any error? Once you install and activate virtualenv, you will create a separate or isolated environment where you can Install different python libraries or modules without touching your old available libraries.

Read this:- How to install Python PIP on Windows, macOS, and Linux

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; else, you may get an error.

$ sudo pip3 install virtualenv
Install virtualenv
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.

Create Virtual Environment
Create Virtual Environment

One last step is to activate the Virtual environment for that pass the below command:

$ source .virtualenvironmentname/bin/activate
Activate Virtual Environment
Activate Virtual Environment

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
Install Selenium using PIP3
Install Selenium using PIP3

You were able to successfully installed selenium in 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

Without chrome driver, selenium will not work. For that reason, you need to Download chrome driver from an official site.

Download Chrome Browser
Download Chrome Browser

Once the page gets loaded, you will see a different release or versions of the chrome driver. To Download chrome driver, first, you need to check which version of Chrome Browser you are using. How to check chrome version? Follow the below step.

Step 1. Click on Click on three dots below close button The three dots which are below the close button.

Step 2. The Next step is to click on Settings.

Click on Settings
Click on Settings

Step 3. Go to About Chrome tab and check the chrome version. In my case chrome version is 89.

Check chrome browser
Check chrome browser

Once you are confirmed about a chrome version, let’s download the chrome driver. In my case, I’ll Download ChromeDriver 89.0.4389.23, and you may download the chrome driver according to the Chrome Browser version.

Select the appropriate version
Select the appropriate version

When you click on a specific chrome driver version, It will redirect 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.

Download Chrome Driver for Linux
Download Chrome Driver for Linux

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/89.0.4389.23/chromedriver_linux64.zip
Download chromeDriver
Download chromeDriver

After this extract, downloaded zip file to driver directory using unzip command with a flag -d

$ unzip chromedriver_linux64.zip -d drivers/
Extract Chrome Driver
Extract Chrome Driver

After that, pass the ls command to verify whether the file is extracted as per our intention or not.

$ ls drivers/chromedriver
Verify Extracted file
Verify Extracted file

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 vimnanoemacs, 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 current project directory; 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('http://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 pass the below command in a terminal:

$ 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.

Web page Automatically run in Chrome Browser
Web page Automatically run in Chrome Browser

The very next moment webpage title will be displayed on your terminal.

Web page Title output inside a terminal
Web page Title output inside a terminal

Wrap Up

That’s all to Download and Setup Selenium on Ubuntu or any Debian-based distributions Machines.

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.

Like what you read? Please share it with others.
  • 4
    Shares
guest
0 Comments
Inline Feedbacks
View all comments