Running Selenium and Chrome on WSL2

Derrick Njobuenwu
3 min readApr 11, 2024

--

Introduction

Automating web tasks and testing web applications have become essential in modern development workflows. Selenium, combined with Chrome, offers a powerful solution for automated browser-based testing. However, setting up Selenium with Chrome on Windows Subsystem for Linux (WSL2) can be challenging. In this guide, I’ll walk through the steps to install, configure, and run Selenium and Chrome on WSL2 using Python and Selenium webdriver.

Step 1: Install WSL2

Before installing Selenium and Chrome, ensure you have WSL2 setup on your system. On Windows 10 version 2004 or higher or Windows 11, execute the following command in your terminal:

wsl --install

This command will take care of enabling the Windows Virtualization Layer, updating the Linux kernel, and installing the default Linux distribution (e.g. Ubuntu 20.04).

After installation, log in to WSL2 by typing wsl in your terminal and press enter.

Next, update the repository and any packages using the command:

sudo apt update && sudo apt upgrade -y

Step 2: Install the Latest Chrome for Testing (for Linux)

As the Chrome version for testing is not available in Ubuntu’s official APT repository, we need to download and install it manually. Execute the following commands:

Install jq for Ubuntu/Debian OS:

sudo apt install jq

Change the working directory to the user home directory:

cd ~

Download the latest Chrome file:

meta_data=$(curl 'https://googlechromelabs.github.io/chrome-for-testing/\
last-known-good-versions-with-downloads.json')
wget $(echo "$meta_data" | jq -r '.channels.Stable.downloads.chrome[0].url')

Install Chrome dependencies:

sudo apt install ca-certificates fonts-liberation \
libappindicator3-1 libasound2 libatk-bridge2.0-0 libatk1.0-0 libc6 \
libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgbm1 \
libgcc1 libglib2.0-0 libgtk-3-0 libnspr4 libnss3 libpango-1.0-0 \
libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 \
libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 \
libxrandr2 libxrender1 libxss1 libxtst6 lsb-release wget xdg-utils -y

Unzip Chrome:

unzip chrome-linux64.zip

Step 3: Install Compatible Chromedriver

Download the latest Chromedriver using the following commands:

meta_data=$(curl 'https://googlechromelabs.github.io/chrome-for-testing/\
last-known-good-versions-with-downloads.json')
wget $(echo "$meta_data" | jq -r '.channels.Stable.downloads.chromedriver[0].url')

Unzip the Chromedriver binary file:

unzip chromedriver-linux64.zip

Step 4: Configure Python and Install Selenium

Before installing Selenium, set up a Python virtual environment. You can use venv , pyenv, conda, or your favourite virtual environment manager.

sudo apt install python3-venv -y
python3 -m venv .venv
source .venv/bin/activate

Install Selenium:

pip install selenium

Step 5: Run Selenium

Now, let’s write a Python script to run Selenium with Chrome. Create a new folder named selenium , create a new file called selenium_code.py and open VSCode:

mkdir -p "selenium" && cd "selenium" && touch selenium_chromedriver_demo.py && code .

Write the Python script (selenium_chromedriver_demo.py):


import os
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

# Define Chrome options
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--no-sandbox")
# Add more options here if needed

# Define paths
user_home_dir = os.path.expanduser("~")
chrome_binary_path = os.path.join(user_home_dir, "chrome-linux64", "chrome")
chromedriver_path = os.path.join(user_home_dir, "chromedriver-linux64", "chromedriver")

# Set binary location and service
chrome_options.binary_location = chrome_binary_path
service = Service(chromedriver_path)

# Initialize Chrome WebDriver
with webdriver.Chrome(service=service, options=chrome_options) as browser:
browser.get("https://www.scrapethissite.com/")

# Wait for the tagline to appear
tagline = browser.find_element(By.CSS_SELECTOR, "#hero > div > div > div > p")
print(f"{tagline.text}")

Run the program:

Go to your terminal and run the command

python3 ~/selenium/selenium_chromedriver_demo.py

Automating the Setup Process with a Bash Script

To streamline the setup process, we can consolidate the aforementioned steps into a simple bash script. Save the following code to a file named ws12_selenium_setup.sh, and make it executable by running chmod +x ws12_selenium_setup.sh

You can find this script ws12_selenium_setup.sh and the Python code selenium_chromedriver_demo.py on GitHub as a repository

--

--