Running Selenium on Windows 10 From Ubuntu WSL
If you’re trying to run Selenium with Chrome headless on Ubuntu WSL and encountering issues, an alternative approach is to install the Selenium Standalone Server on your Windows 10 machine and then connect to it remotely from the Ubuntu WSL terminal. This can help you overcome the limitations of running Chrome headless directly within WSL.
Here’s a step-by-step guide on how to achieve this:
Step 1: Set Up Selenium Standalone Server on Windows 10
-
Install Java: Ensure you have Java installed on your Windows 10 machine since Selenium requires it to run. You can download and install Java from the official website.
-
Download Selenium Standalone Server: Visit the Selenium Downloads page and download the latest version of the Selenium Standalone Server (selenium-server-standalone-x.xx.x.jar).
-
Start Selenium Server: Open Command Prompt on Windows and navigate to the directory where you’ve downloaded the Selenium Standalone Server JAR file. Run the following command to start the server:
1
java -jar selenium-server-standalone-x.xx.x.jar
Make sure to replace
x.xx.x
with the actual version number you’ve downloaded.
The Selenium Server should now be running and listening for incoming WebDriver requests.
Step 2: Connect from Ubuntu WSL
-
Install Dependencies: In your Ubuntu WSL terminal, you’ll need to install the required dependencies, including Python and the Selenium Python package. If you haven’t already, you can do this using:
1 2 3
sudo apt update sudo apt install python3 python3-pip pip3 install selenium
-
Write Python Script: Create a Python script (e.g.,
remote_selenium.py
) with the following content:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
from selenium import webdriver # Replace 'YOUR_WINDOWS_IP' with the actual IP address of your Windows 10 machine windows_ip = 'YOUR_WINDOWS_IP' windows_port = 4444 # Default port for Selenium Server # Set up Chrome options chrome_options = webdriver.ChromeOptions() chrome_options.add_argument('--headless') chrome_options.add_argument('--no-sandbox') chrome_options.add_argument('--disable-dev-shm-usage') # Connect to the remote Selenium Server remote_url = f'http://{windows_ip}:{windows_port}/wd/hub' driver = webdriver.Remote(command_executor=remote_url, desired_capabilities=chrome_options.to_capabilities()) # Now you can use 'driver' to interact with the remote Chrome instance # For example: driver.get('https://www.example.com') print(driver.title) # Don't forget to close the driver driver.quit()
Replace
'YOUR_WINDOWS_IP'
with the actual IP address of your Windows 10 machine. -
Run the Script: Execute the Python script using Python 3 in your Ubuntu WSL terminal:
1
python3 remote_selenium.py
This setup allows you to run Selenium-driven Chrome headless instances remotely from your Windows 10 machine while interacting with them from your Ubuntu WSL terminal.
Remember that this approach requires network connectivity between your WSL instance and Windows machine. Also, ensure that any firewalls or security settings don’t block the communication between them.