How To Install And Set Up Selenium With Python And Ubuntu
- With Code Example
- July 29, 2024
Learn what Selenium is, Install Python & PIP, Install and Run Selenium
Series - Python Selenium
Good day, Developers! I’ll talk about Selenium and how to install Selenium on your Ubuntu operating system. I’ll try to address some fundamental questions about Python and Selenium automation with Ubuntu as the operating system. Let’s get this party started right now.
Table of Contents
What is Selenium?
Selenium is an open-source umbrella project for web browser automation, it provides tools and libraries for automation and web scrapping. It is broadly used for automation testing web applications. Selenium is used for the following operations:
- Cross-browser automation testing
- Web scrapping
- Web automation
Selenium is available in many languages such as JavaScript (Node. js), Python, Ruby, Java, Kotlin, and C#. I am going to use it with the Python programming language. To get started with it we need to install Python in our Ubuntu system.
Install Python and Pip in Ubuntu
Although Ubuntu comes with pre-installed the latest version of Python. you can check by running python3 -v
from your terminal and it will be returned the installed version of Python. If it is not installed You have to follow the below steps.
To install Python 3.8 in our Ubuntu system we have run the following commands from the terminal. To open the terminal press ctrl+alt+t
keys and type the following one by one:
sudo apt-get update
sudo apt -y upgrade
python3 -V
If you get Python 3.*.*
on your terminal, that means the Python is already installed and you need not perform any additional steps. If it is not fo you have to perform the following actions:
sudo apt-get install python3.8 -y
This will install Python 3.8 in Ubuntu you can check and verify by running python3 -v
the command. Now its time to install PIP (Python package manager)
sudo apt-get install python3-pip -y
Install Selenium package
To install the Selenium Python package we have to run the following command from the terminal:
pip3 install selenium
To learn more about the Selenium package for Python click here.
Download Chrome driver
Now you have to download the chrome driver as per your chrome browser installed in our system. To see your chrome version click on 3 dots from the top right corner > Help > About Google Chrome or just click here.
Once you have your Chrome browser now you can navigate this website and download your driver.
Python Script
This is the final step in which we will finally run automation and perform some actions. You can learn in more detail from here.
import time
from selenium import webdriver
driver = webdriver.Chrome('/usr/local/bin/chromedriver') # Optional argument, if not specified will search path.
driver.get('http://www.google.com/')
time.sleep(5) # Let the user actually see something!
search_box = driver.find_element_by_name('q')
search_box.send_keys('ChromeDriver') # typing in serach box
search_box.submit() # serach query
time.sleep(5) # Let the user actually see something!
driver.quit() # quit and close
Code sample available here
Working Demo 👇
Screen recording
Final words
We have learned some basics about getting started with Selenium with Python on the Ubuntu operating system. I hope this will help to get started with Selenium.
Note: Many websites do not allow automation, as we all know, and I am not advocating for it. This article was prepared only for the sake of education.