Henry Poon's Blog

Running Selenium Webdriver on WSL2

This is a newer version of the post that I made before on how to run Selenium on WSL, but this one is on doing it in WSL2. The instructions are similar at a high level but there are some additional gotchas.

You will need:

How it works

VcXsrv starts up an X server on the Windows host machine, and what WSL will do is connect to this server to pass on the details of what programs need to be displayed (in this case, the browser window). Geckodriver is there purely as a middle layer for Selenium to be able to interact with Firefox.

VcXsrv setup

This setups the X server so that Windows can display graphical data coming from the Ubuntu instance.

  1. Download and install
  2. Start it up with the the command line parameter “-ac”. This flag removes access control restrictions for connecting clients (which is okay as long as this server isn’t exposed on the open Internet and that the only client connecting is the Ubuntu instance)
  3. Allow connections to VcXsrv on Windows Firewall

The “-ac” flag and allowing the program through Windows Firewall is pretty important. When I didn’t do this, Ubuntu would give me cryptic error messages like “Broadway display type not supported”, but once I set up these access controls, everything was fine.

Bash setup

Install firefox

sudo apt install firefox

Export DISPLAY variable in .bashrc (or for whatever terminal you use, maybe .zshrc, etc.)

export DISPLAY=$(ip route | awk '{print $3; exit}'):0

This will configure your DISPLAY variable to use the IP address listed in the resolv.conf file. The IP address listed there is the IP address of the X server that will be used for displaying the browser window.

Start up VcXsrv as described in the above and then try to open Firefox. The Firefox window should open.

Geckodriver setup

This acts purely as the API interface that allows Selenium to interact with Firefox. Selenium looks for this executable when controlling the web browser.

  1. Download geckodriver (make sure to get the Linux one and not the Windows one)
  2. Put it in /usr/bin (or alternatively some other folder in your PATH)

Selenium sample on Python

I tend to use Python when running Selenium code, and I haven’t tried it on another language (I imagine the principles are similar). Here is a piece of sample code for starting up the browser and opening up a website, but really any Selenium code can be used.

import time

from selenium import webdriver
from selenium.webdriver import DesiredCapabilities

def execute_with_retry(method, max_attempts):
    e = None
    for i in range(0, max_attempts):
        try:
            return method()
        except Exception as e:
            print(e)
            time.sleep(1)
    if e is not None:
        raise e

capabilities = DesiredCapabilities.FIREFOX
capabilities["marionette"] = True
firefox_bin = "/usr/bin/firefox"
browser = execute_with_retry(lambda: webdriver.Firefox(
    firefox_binary=firefox_bin, capabilities=capabilities), 10)

browser.get("https://www.google.com")

time.sleep(10)

browser.close()

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Next Post

Previous Post

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© 2024 Henry Poon's Blog

Theme by Anders Norén