Selenium is a versatile tool that can be used for automating browser-based tests. It has a wide range of features that make it an ideal choice for automating tests. Selenium can be used to automate tests for web applications and web services. Selenium supports a number of programming languages, including Java, C#, Python, and Ruby.
This makes it possible to write tests in the language that you are most comfortable with. In addition, Selenium has a large user community that provides support and help when needed.
In this blog post, you will learn to set up a Selenium environment on an Ubuntu system. Also provides you with a few examples of Selenium scripts written in Python.
You must have Sudo privileged account access to the Ubuntu system.
One of the examples also required a desktop environment to be installed.
Use the below steps to install the latest Google Chrome browser on Ubuntu and Debian systems.
wget -nc https://dl-ssl.google.com/linux/linux_signing_key.pubcat linux_signing_key.pub | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/linux_signing_key.gpg >/dev/null
sudo sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/chrome.list'
sudo apt updatesudo apt install google-chrome-stable
Press ‘y’ for all the confirmation asked by the installer.
This will install Google Chrome on your Ubuntu system.
We will use a virtual environment for running Python scripts. Follow the below steps to create Python virtual environment and install the required python modules.
mkdir tests && cd tests
python3 -m venv venvsource venv/bin/activate
Once the environment is activated, You will find the updated prompt as below screenshot:
pip install selenium webdriver-manager
Your system is ready to run Selenium scripts written in Python. Now, create a sample selenium script in Python that fetches the title of a website.
This script will run headless, So you can run it without an X desktop environment. You can simply SSH to your system and run the below example:
nano test.py
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
options = Options()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
driver.get("https://python.org")
print(driver.title)
driver.close() Press CTRL + O to save content to file and press CTRL + X to close editor.
python test.py
You will see the output something like below:
In order to run this example, the Ubuntu system must have installed a Desktop environment. If the desktop is not installed, use another tutorial to install the Desktop environment on Ubuntu systems.
Now, log in to the desktop interface and try to run the below example.
nano test.py
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager
options = Options()
# options.add_argument('--headless')
# options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
driver.get('http://www.google.com')
search = driver.find_element(by=By.NAME, value="q")
search.send_keys("Hey, Tecadmin")
search.send_keys(Keys.RETURN)
time.sleep(5)
driver.close() Write the changes to file with CTRL + O and close this with keyboard shortcut CTRL + X
python test2.py
You will see that a Browser window will open and perform the defined tasks in the script. See the below screencast of the run:
In this tutorial, you have learned about the configuration of Selenium for Python on Ubuntu and Debian Linux systems. Also provides you with two Selenium examples. Hope this tutorial helps you to understand to run Selenium with Python.
The post Setup Selenium with Python and Chrome on Ubuntu & Debian appeared first on TecAdmin.
Official Ubuntu support for the NVIDIA Rubin platform, including the NVIDIA Vera Rubin NVL72 rack-scale…
CES 2026 is here, bringing together the technologies defining the next generation of connected devices.…
Here’s the guide to deploy Elastic Stack on Ubuntu VPS, with secure access, HTTPS proxying,…
This guide walks through deploying Nagios Core on an Ubuntu VPS, from system prep to…
After a decade under Pablo Cantero's stewardship, Shoryuken has a new maintainer - me. I'm…
MAAS 3.7 has been officially released and it includes a bunch of cool new features.…