20

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?

Ñhosko
  • 723
  • 2
  • 8
  • 25
  • Did you find the solution for this? – vftw Aug 10 '21 at 16:55
  • @LuísCosta You mentioned elsewhere that you tried [this link](https://www.gregbrisebois.com/posts/chromedriver-in-wsl2/) from the answer below. What happened when you attempted to install the Linux version of Chrome? I would recommend following up on that path, since as I mentioned in the other comment, the `localhost` problem mentioned in that link is going to stop you from doing it the WSL1 way (which is what both you and this OP seem to be trying). As a possible alternative, is there a way to change the host/port that Selenium is attempting to connect to? – NotTheDr01ds Aug 10 '21 at 19:32
  • I created a fresh copy of WSL (to get rid of possible misconfiguration), followed the link below and had no success, no matter what I try I always end up with: Message: unknown error: Chrome failed to start: exited abnormally. (unknown error: DevToolsActivePort file doesn't exist) (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed Already discarded the previous approach because of the problem you pointed out :) – vftw Aug 10 '21 at 20:08
  • @LuísCosta Don't forget to tag me in replies - I don't get notified otherwise (unless it's my question or answer). Only remembered to check back after our last exchange on your other question! Let me see if I can set it up. It's been about a year since I tried ChromeDriver on WSL2, and I can't remember where I ended up. – NotTheDr01ds Aug 11 '21 at 20:08
  • @LuísCosta Are you still working on this? I was able to get this working by (a) installing xrdp per [these steps](https://stackoverflow.com/a/68856351/11810933) (although that shouldn't be any different than install an X server for the end result), and then (b) following the instructions linked in the answer below. If you still want to troubleshoot this, can you do a `command -v chromedriver` and a `command -v google-chrome` and post the results? Thanks! – NotTheDr01ds Aug 24 '21 at 14:48

4 Answers4

6

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
Ruben Santana
  • 411
  • 5
  • 10
  • On Greg Brisebois's tutorial, to which you've linked, the line where he greps the nameserver entry from the resolve.conf file actually returns IP address of my DHCP server, and not the IP address of the Windows Host machine – ChillyPenguin Jan 28 '21 at 03:43
  • @ChillyPenguin I could not tell you what error you may have, maybe you are associating the IP addresses, and you think it comes from DHCP, in my case the address of the Windows Host machine is constantly changing, sometimes it starts at 172 and sometimes at 192. On the other hand, I have to configure the DISPLAY variable before opening the xlaunch and also the IDE (VS Code in my case) so that it can run. Ultimately correcting your particular situation may be found in another post. – Ruben Santana Jan 28 '21 at 07:06
  • 1
    I would just like to add what worked for me so that it will help others who faced the same problem. 1. I followed the tutorial mentioned in the answer above to install both Chrome and the Chrome driver in my WSL. I did not install the X Server. 2. In my test code, I have the following two lines which made the test headless. `options = webdriver.ChromeOptions()` `options.headless = True` I hope this helps. – WJ Chua Sep 06 '21 at 14:30
  • 1
    I'll add it here so that it could help someone. I was getting the error `[0109/114414.109442:ERROR:exception_handler_server.cc(361)] getsockopt` and the problem was that I was running WSL 1. I upgraded to 2 with `wsl --set-version 2` (from windows power shell) – Michele Piccolini Jan 09 '23 at 17:20
3

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.

kmpatel100
  • 108
  • 5
0

I don't think it's possible. You're under Linux here, so you can't used a Windows executable.

I tried using the headless version of Chromium, but it would not work, because (it seems) Q

Bibelo
  • 221
  • 2
  • 8
  • 2
    It is possible to run windows executables from WSL 2 as per official documentation from Windows: [link](https://learn.microsoft.com/en-us/windows/wsl/interop#run-windows-tools-from-linux). "WSL can run Windows tools directly from the WSL command line using [tool-name].exe. For example, notepad.exe... Windows executables run in WSL are handled similarly to native Linux executables -- piping, redirects, and even backgrounding work as expected." – Ñhosko Sep 17 '20 at 19:50
0

https://www.selenium.dev/documentation/grid/getting_started/

You can use grid to solve this problem

In windows:

  1. install the driver and config the path
  2. install the java11 or higher installed
  3. install the selenium-server

In wsl2-Ubuntu20.04:

  1. pip install selenium
pig black
  • 1
  • 2