Search code examples
visual-studio-codeselenium-webdriverwebdriverchromium

Chromedriver gives an error when trying to run a Python script in Github's VS Code remote codespace


I organized a Github VS Code codespace to work on a project which uses selenium package and chrome webdriver.

I added selenium package and installed chrome webdriver via terminal by using:

pip install selenium
sudo apt install chromium-chromedriver

When I try to run the following script:

import time

from selenium import webdriver
from selenium.webdriver.common.by import By

options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome('chromedriver', options=options)

driver = webdriver.Chrome("/usr/bin/chromedriver", options=options)

<...>

I get the following error:

Traceback (most recent call last):
  File "/workspaces/swordy/selenium_course/first.py", line 10, in <module>
    driver = webdriver.Chrome('chromedriver', options=options)
  File "/usr/local/python/3.10.4/lib/python3.10/site-packages/selenium/webdriver/chrome/webdriver.py", line 80, in __init__
    super().__init__(
  File "/usr/local/python/3.10.4/lib/python3.10/site-packages/selenium/webdriver/chromium/webdriver.py", line 101, in __init__
    self.service.start()
  File "/usr/local/python/3.10.4/lib/python3.10/site-packages/selenium/webdriver/common/service.py", line 104, in start
    self.assert_process_still_running()
  File "/usr/local/python/3.10.4/lib/python3.10/site-packages/selenium/webdriver/common/service.py", line 117, in assert_process_still_running
    raise WebDriverException(f"Service {self.path} unexpectedly exited. Status code was: {return_code}")
selenium.common.exceptions.WebDriverException: Message: Service chromedriver unexpectedly exited. Status code was: 1

I'm new to managing codespaces and don't have enough experience to fix this yet. I expected it to run a chrome window in which the following commands would execute.


Solution

  • Ok, so getting a little deeper into the weeds I found out that:

    Github's remote VS Code codespace utilizes Ubuntu and has bash terminal. So to run a Google Chrome browser I need to download and install compatible versions of Chrome browser and chromedriver. Then by passing the path to the downloaded chromedriver to webdriver.Chrome() I can finally run an instance of Chrome browser which would utilize the Linux environment. The following link explains how it's done: https://cloudbytes.dev/snippets/run-selenium-and-chrome-on-wsl2

    BUT It looks like that wouldn't be enough for the remote codespace. After going through instructions in the link above, I encountered new errors:

    Traceback (most recent call last):
      File "/workspaces/swordy/selenium_course/first.py", line 21, in <module>
        driver = webdriver.Chrome(service=webdriver_service, options=options)
      File "/usr/local/python/3.10.4/lib/python3.10/site-packages/selenium/webdriver/chrome/webdriver.py", line 80, in __init__
        super().__init__(
      File "/usr/local/python/3.10.4/lib/python3.10/site-packages/selenium/webdriver/chromium/webdriver.py", line 104, in __init__
        super().__init__(
      File "/usr/local/python/3.10.4/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 286, in __init__
        self.start_session(capabilities, browser_profile)
      File "/usr/local/python/3.10.4/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 378, in start_session
        response = self.execute(Command.NEW_SESSION, parameters)
      File "/usr/local/python/3.10.4/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 440, in execute
        self.error_handler.check_response(response)
      File "/usr/local/python/3.10.4/lib/python3.10/site-packages/selenium/webdriver/remote/errorhandler.py", line 245, in check_response
        raise exception_class(message, screen, stacktrace)
    selenium.common.exceptions.WebDriverException: 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.)
    Stacktrace:
    #0 0x55e1d6f44d93 <unknown>
    #1 0x55e1d6d132d7 <unknown>
    #2 0x55e1d6d3bab0 <unknown>
    #3 0x55e1d6d37a3d <unknown>
    #4 0x55e1d6d7c4f4 <unknown>
    #5 0x55e1d6d73353 <unknown>
    #6 0x55e1d6d42e40 <unknown>
    #7 0x55e1d6d44038 <unknown>
    #8 0x55e1d6f988be <unknown>
    #9 0x55e1d6f9c8f0 <unknown>
    #10 0x55e1d6f7cf90 <unknown>
    #11 0x55e1d6f9db7d <unknown>
    #12 0x55e1d6f6e578 <unknown>
    #13 0x55e1d6fc2348 <unknown>
    #14 0x55e1d6fc24d6 <unknown>
    #15 0x55e1d6fdc341 <unknown>
    #16 0x7fae17a5a609 start_thread
    

    Ok, for some reason Chrome won't start. So I decided I would try to at least run a chrome browser from bash:

    google-chrome-stable --no-sandbox
    [5021:5021:0308/070932.768189:ERROR:ozone_platform_x11.cc(238)] Missing X server or $DISPLAY
    [5021:5021:0308/070932.768255:ERROR:env.cc(255)] The platform failed to initialize.  Exiting.
    [5021:5042:0308/070932.771735:ERROR:bus.cc(399)] Failed to connect to the bus: Failed to connect to socket /var/run/dbus/system_bus_socket: No such file or directory
    [0308/070932.794700:ERROR:nacl_helper_linux.cc(355)] NaCl helper process running without a sandbox!
    Most likely you need to configure your SUID sandbox correctly
    

    There are a bunch of questions online from people who encountered the same errors by running puppeteer, docker etc., but I couldn't find the right solution. I suspect that the reason for the new errors is the fact that remote codespace doesn't have a GPU and because of that can't launch anything that requires it. It is best demonstrated by trying to run xeyes in bash terminal (or by adding --headless argument when running chrome, but you wouldn't see anything, so what's the point?).

    So, what are the options here? First, I'd say that the link provided above works perfectly well for the desktop version of VS Code. You can use the windows cmd terminal and won't even need to worry about linux chrome or chromedriver, or you can install WSL and then go through the steps in that link. At least that's how I'm going to solve this problem

    Resulting code for desktop VS Code:

    import time
    
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.chrome.service import Service
    from selenium.webdriver.common.by import By
    
    options = Options()
    options.add_argument("--no-sandbox")
    webdriver_service = Service(f"/chromedriver/stable/chromedriver")
    driver = webdriver.Chrome(service=webdriver_service, options=options)
    

    Second, I guess there is a way to try and use your own PC to act as a Display/GPU for the remote codespace. But that is not something I'm capable to do atm I think these links would be the right direction to investigate?: https://www.gregbrisebois.com/posts/chromedriver-in-wsl2/ https://manpages.ubuntu.com/manpages/xenial/man1/xvfb-run.1.html

    If anyone knows how to run gpu-dependent applications in Github's virtual VS Code codespace, please leave a comment!

    ================================

    Update: Ok, as I thought, the last piece of puzzle would be to run the gui-dependent applications by using your PC, and to be more specific, VcXsrv. This link explains how to do this:

    https://babyprogrammer.com/blog/running-gui-code-applications-in-github-codespaces-on-windows/

    By combining all the info I found, I was able to run chrome from remote codespace. Hope all the info helps anyone with the same struggles!