Henry Poon's Blog

Running Selenium Webdriver on Bash for Windows

NOTE: This article is for WSL1, for WSL2, see this post

Bash for Windows has been working pretty great for me until I needed to run Selenium Webdriver on it. I quickly learned that it wouldn’t work just work right out of the box, and the set up for it is quite convoluted.

You will need

Bash setup

Install Firefox:

sudo apt-get install firefox

Export DISPLAY variable in ~/.bashrc. Just add this to the ~/.bashrc:

export DISPLAY=:0

Xming setup

Just download it from the link above and run it.

Download geckodriver

Download geckodriver from the link above and put in the /usr/bin/ folder in Bash for Windows.

Running Selenium

Here is a piece of sample Python code I used to setup the web browser. It will setup the browser, open Google, sit there for 10 seconds and then quit.  Make sure to have Xming running otherwise the browser isn’t going to start.

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

2 Comments

  1. gary 2019-06-09

    Thanks for this.
    Been trying to troubleshoot my junk and this worked. You’re the only one that mentioned xming.

  2. Adam 2021-04-28

    Hey,
    Thanks a lot for this.

    I was having troubles with the proper Firefox version, it worked for me with v66.01. You can get it from here:
    https://ftp.mozilla.org/pub/firefox/releases/66.0.2/

    And this explains how to install the .tar from bash:
    https://www.xmodulo.com/how-to-install-old-firefox-on-linux.html

    After that this works like magic!

Leave a Reply to AdamCancel 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