Running Selenium Webdriver on Bash for Windows
This post is recreated from the original at https://blog.henrypoon.com/blog/2017/06/18/running-selenium-webdriver-on-bash-for-windows/
NOTE: This article is for WSL1. For WSL2, see my updated post: Running Selenium Webdriver on WSL2
Bash for Windows had been working great for me until I needed to run Selenium WebDriver on it. I quickly learned that it would not work right out of the box, and the setup for it is quite convoluted.
You will need:
- Bash for Windows
- Xming - This allows the browser window to appear on the screen
- geckodriver - Selenium needs this to run Firefox
Bash setup
Install Firefox:
1
sudo apt-get install firefox
Export the DISPLAY variable in ~/.bashrc. Just add this to ~/.bashrc:
1
export DISPLAY=:0
Xming setup
Download Xming from the link above and run it.
Download geckodriver
Download geckodriver from the link above and place it in the /usr/bin/ folder in Bash for Windows.
Running Selenium
Here is a piece of sample Python code I used to set up the web browser. It will set up the browser, open Google, sit there for 10 seconds, and then quit. Make sure to have Xming running, otherwise the browser will not start.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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()