Run Selenium and Chrome on WSL2 using Python and Selenium webdriver
Selenium combined with Headless Chrome is a great tool for creating automated UI tests for web applications. With Selenium libraries, Python can be used to create and run automated browser-based tests & tasks.
This guide will show you how to install, configure and run Selenium and Chrome on WSL2 using Python and Selenium webdriver.
Step 1: Install WSL2
On Windows 10 version 2004 or higher (Build 19041 and above) or windows 11, run the below.
wsl --install
This will take care of all the steps required, i.e.
- Enable Windows Virtualisation Layer and WSL2
- Update the Linux kernel to the latest version
- Install the default Linux distribution, i.e. latest Ubuntu (Currently Ubuntu 20.04)
Then type wsl
in your terminal and press enter to login to WSL2.
NOTE: All codeblocks below are formatted as multi-line commands so the entire block needs to be copy pasted and not line by line.
Ensure you go to your home directory, update the repository and any packages
a) Change the working directory to the user home directory.
cd "$HOME"
b) Update the repository and any packages
sudo apt update && sudo apt upgrade -y
Step 2: Install latest Chrome for Linux
Chrome is not available in Ubuntu's official APT repository, so we will download the .deb directly from Google and install it.
a) Download the latest chrome .deb file
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
b) Install the .deb file
sudo dpkg -i google-chrome-stable_current_amd64.deb
c) And finally, force install all the dependencies by running
sudo apt --fix-broken install
This feels a hackish way of installing the latest version of Chrome, but if someone figures out a better way, do let me know please.
d) Get the latest version of Chrome
google-chrome-stable --version
In this case, it was 95.0.4638.69
.
Step 3: Install compatible Chromedriver
To be able to run Chrome programmatically, we need to install a compatible Chromedriver. For every version of Chrome, e.g. 95.0.4638.69
, there is a corresponding version of Chromedriver with same version number.
a) You can confirm the Chromedriver version
chrome_driver=$(curl "https://chromedriver.storage.googleapis.com/LATEST_RELEASE") && \
echo "$chrome_driver"
b) Download the latest Chromedriver
curl -Lo chromedriver_linux64.zip "https://chromedriver.storage.googleapis.com/\
${chrome_driver}/chromedriver_linux64.zip"
c) Install unzip
sudo apt install unzip
d) Unzip the binary file and make it executable
mkdir -p "chromedriver/stable" && \
unzip -q "chromedriver_linux64.zip" -d "chromedriver/stable" && \
chmod +x "chromedriver/stable/chromedriver"
Step 4: Configure Python and Install Selenium
Selenium webdriver is available as a Python package, but before installation we need to do some prep.
Configure a Python virtual environment
Run python3 --version
and note the version, e.g. in my case I get the version Python 3.8
Next, we need to install venv
, choose the Python version based on what you have installed.
sudo apt install python3.8-venv -y
Then create a virtual environment
python3 -m venv .venv
Finally, activate the virtual environment by running
source .venv/bin/activate
You should see your terminal change to the below with (.venv)
in the prompt.
Install Selenium
After activating the virtual environment, install Selenium using the pip command.
pip install selenium
It is necessary to activate the Python virtual environment by running
source .venv/bin/activate
before executing the above command
Step 5: Run Selenium
Finally we're ready to start running our automated tests. We write a simple Python script to run Selenium and Chrome/Chromium.
Create a new folder, selenium
and open VSCode by running the below
mkdir -p "selenium" && cd "selenium" && code .
a) The Python program
"""
# Filename: run_selenium.py
"""
## Run selenium and chrome driver to scrape data from cloudbytes.dev
import time
import os.path
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
## Setup chrome options
chrome_options = Options()
chrome_options.add_argument("--headless") # Ensure GUI is off
chrome_options.add_argument("--no-sandbox")
# Set path to chromedriver as per your configuration
homedir = os.path.expanduser("~")
webdriver_service = Service(f"{homedir}/chromedriver/stable/chromedriver")
# Choose Chrome Browser
browser = webdriver.Chrome(service=webdriver_service, options=chrome_options)
# Get page
browser.get("https://cloudbytes.dev")
# Extract description from page and print
description = browser.find_element(By.NAME, "description").get_attribute("content")
print(f"{description}")
#Wait for 10 seconds
time.sleep(10)
browser.quit()
b) Run the program
Go back to your terminal and type python3 selenium/run_selenium.py
Creating a script to automate the process
We can merge the above steps to create a simple bash script to help you automate this entire process. Save this to a file named install-selenium.sh
and then make it executable by running chmod +x install-selenium
.
#!/usr/bin/bash
echo "Changing to home directory..."
pushd "$HOME"
echo "Update the repository and any packages..."
sudo apt update && sudo apt upgrade -y
echo "Install prerequisite packages..."
sudo apt install wget curl unzip -y
echo "Download the latest Chrome .deb file..."
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
echo "Install Google Chrome..."
sudo dpkg -i google-chrome-stable_current_amd64.deb
echo "Fix dependencies..."
sudo apt --fix-broken install -y
chrome_version=($(google-chrome-stable --version))
echo "Chrome version: ${chrome_version[2]}"
chromedriver_version=$(curl "https://chromedriver.storage.googleapis.com/LATEST_RELEASE")
echo "Chromedriver version: ${chromedriver_version}"
if [ "${chrome_version[2]}" == "$chromedriver_version" ]; then
echo "Compatible Chromedriver is available..."
echo "Proceeding with installation..."
else
echo "Compabible Chromedriver not available...exiting"
exit 1
fi
echo "Downloading latest Chromedriver..."
curl -Lo chromedriver_linux64.zip "https://chromedriver.storage.googleapis.com/${chromedriver_version}/chromedriver_linux64.zip"
echo "Unzip the binary file and make it executable..."
mkdir -p "chromedriver/stable"
unzip -q "chromedriver_linux64.zip" -d "chromedriver/stable"
chmod +x "chromedriver/stable/chromedriver"
echo "Install Selenium..."
python3 -m pip install selenium
popd
Alternatively, this script is also available on GitHub as a repository.