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 for Windows
- Xming – This will actually allow the Window to appear on the screen
- geckodriver – Selenium now needs this to run Firefox
sudo apt-get install firefox
Export DISPLAY variable in ~/.bashrc. Just add this to the ~/.bashrc:
Just download it from the link above and run it.
Download geckodriver from the link above and put in the /usr/bin/ folder in Bash for Windows.
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()
gary June 9, 2019
Thanks for this.
Been trying to troubleshoot my junk and this worked. You’re the only one that mentioned xming.
Adam April 28, 2021
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:
And this explains how to install the .tar from bash:
After that this works like magic!