Home > Enterprise >  Unable start Chrome in headless mode
Unable start Chrome in headless mode

Time:05-31

Using Windows Subsystem WSL2. I am trying to open browser in Ubuntu 20.04.

Followed commands to install google chrome and chrome-driver

https://tecadmin.net/setup-selenium-chromedriver-on-ubuntu/

Facing issue while starting chrome via terminal. Unable to initialise browser as well.

Versions:

  1. Windows 10
  2. Ubuntu 20.04
  3. Google Chrome 102.0.5005.61
  4. ChromeDriver 102.0.5005.61
  5. selenium-webdriver (4.1.0)
  6. watir (7.1.0)

When I tried to open google-chrome via terminal.

$google-chrome

Error: [0530/135205.172753:ERROR:exception_handler_server.cc(361)] getsockopt: Invalid argument (22)

$sudo update-alternatives --config x-www-browser


Selection    Path                           Priority   Status
------------------------------------------------------------
 * 0          /usr/bin/google-chrome-stable   200       auto mode
   1          /usr/bin/chromium-browser       40        manual mode
   2          /usr/bin/google-chrome-stable   200       manual mode        
   3          /usr/bin/wslview                30        manual mode

I tried to initialise browser in IRB as well.

require 'watir'
browser = Watir::Browser.new(:chrome)

Error: Net::ReadTimeout

Tried reinstalling subsystem as well. Still facing same issue.

CodePudding user response:

A few possible issues:

  • First, you mention that you are on WSL2, but the Chrome error you are receiving leads me to believe that you might be on WSL1. I see the same error if I run google-chrome on WSL1, but not on WSL2.

    Double-check that with wsl.exe -l -v. You may need to convert the instance to WSL2 with wsl --set-version <distro> 2.

  • Second, running google-chrome like that, even on WSL2, will require an X server. WSL in Windows 10 does not support GUI applications, so you would need to install and configure an X server in Windows. You'll find other questions and answers on that topic, so I won't spend much time on it here, since your real question is about running Chrome headless.

  • As long as you are running WSL2 you should be able to run Chrome headless with Watir. It looks like the main thing you are missing is not calling Watir with the headless: true option for Chrome per this doc.

    Here are the steps I took with Watir and Chromedriver. Note that the first line is a fixed version of the directions you linked to, since it requires sudo apt-key add rather than sudo curl:

    curl -sS -o - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add
    sudo bash -c "echo 'deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main' >> /etc/apt/sources.list.d/google-chrome.list"
    sudo apt update
    sudo apt install -y google-chrome-stable
    google-chrome --version # To check the Chromedriver version to download
    cd ~
    wget https://chromedriver.storage.googleapis.com/102.0.5005.61/chromedriver_linux64.zip
    
    unzip chromedriver_linux64.zip
    mv chromedriver ~/.local/bin # A directory on your path
    
    gem install --user-install watir 
    
    irb
    

    In irb:

    require 'watir'
    browser = Watir::Browser.new :chrome, headless: true
    browser.goto 'https://stackoverflow.com/q/72432711/11810933'
    puts browser.title
    

    Which returns the title of your question here.

    It's also possible to use the headless package, as noted on this page.

    Add the following:

    sudo apt install xvfb
    gem install --user-install headless
    

    Then in irb:

    require 'watir'
    require 'headless'
    
    headless = Headless.new
    headless.start
    
    browser = Watir::Browser.new(:chrome)
    browser.goto 'https://stackoverflow.com/q/72432711/11810933'
    puts browser.title
    
    headless.destroy
    
  • Related