I'm trying to headless open Chrome from WSL2 (Ubuntu 18.04) using python 3.
On Windows I'm using Chrome 84. I've downloaed Chrome Driver 84 from ChromeDriver - WebDriver for Chrome. And installed the .exe under C:\ChromeDriver\chromedriver.exe
I've set a symbolic link from my Windows Chrome and ChromeDriver to WSL2:
sudo ln -s '/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe' /usr/bin/google-chrome
sudo ln -s /mnt/c/ChromeDriver/chromedriver.exe /usr/bin/chromedriver
Both Chromes are set to be executable by any user on WSL2.
On WSL2, when I enter in the console:
google-chrome --use-gl=swiftshader
Chrome starts on windows.
Here is my script:
from selenium import webdriver
browser = webdriver.Chrome() # fails
# browser = webdriver.Chrome('/usr/bin/chromedriver') fails
# browser = webdriver.Chrome('/mnt/c/ChromeDriver/chromedriver.exe') fails
browser.get('https://stackoverflow.com')
It fails with error:
raise WebDriverException("Can not connect to the Service %s" % self.path) selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service chromedriver (* OR /usr/bin/chromedriver OR /mnt/c/ChromeDriver/chromedriver.exe depending on how I start webdriver.Chrome())
How to be able to start Chrome Driver from WSL2 using python3 and selenium?
Now we need to move ChromeDriver somewhere that Python and Selenium will be able to find it (a.k.a. in your PATH ). The easiest place to put it is in C:\Windows .
For those who have not yet found the solution. Follow this tutorial: chromedriver in WSL2 Many are similar, but what did the trick for me was to place the chromedriver in the corresponding group and user:
sudo chown root:root /usr/bin/chromedriver
you can install chromedrive by given code.
wget -N http://chromedriver.storage.googleapis.com/2.26/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
chmod +x chromedriver
sudo mv -f chromedriver /usr/local/share/chromedriver
sudo ln -s /usr/local/share/chromedriver /usr/local/bin/chromedriver
sudo ln -s /usr/local/share/chromedriver /usr/bin/chromedriver
you do need chrome if you don't have it use given code.
wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
echo 'deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main' | sudo tee /etc/apt/sources.list.d/google-chrome.list
sudo apt-get update
sudo apt-get install google-chrome-stable
I might missing something so, please see reference site. reference: https://www.srcmake.com/home/selenium-python-chromedriver-ubuntu
After you get selenium and chrome driver you can use given code for headless chrome. Also, there is one package call "chromedriver_autoinstaller" I am not sure it is working on ubuntu or not but it's great package if you are using same script everyday and your browser is on auto-update.
code for headless chrome:
#for headless browser use this arguments
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--window-size=1920x1080")
driver = webdriver.Chrome(chrome_options=chrome_options)
put necessary arguments in webdriver.Chrome if you are using path and other conditions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With