15

I am trying to use the new (2016) headless version of Chromium with Selenium/ChromeDriver (In the past, I used Firefox with xfvb but this promises to be much better).

I have compiled a headless version of Chromium from sources (I did not find any pre-built binaries) based on the instructions I found here and then I used the following code to launch it through Selenium:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

l_option = Options()
l_option.add_argument('headless')
l_option.add_argument('disable-notifications')
l_option.binary_location = '/home/fi11222/Headless_Chromium/headless_shell'
l_driver = webdriver.Chrome(chrome_options=l_option)

The same code works with the standard chromium (if I remove the binary.location option)

As is, however, I get the following error:

selenium.common.exceptions.WebDriverException: Message: unknown error: unrecognized Chrome version: HeadlessChrome/59.0.3032.0
  (Driver info: chromedriver=2.27.440175 (9bc1d90b8bfa4dd181fbbf769a5eb5e575574320),platform=Linux 4.4.0-53-generic x86_64)

Apparently, the headless chromium binary is compiled with a version ID that ChromeDriver does not recognize. Help !!

Environment:

  • Compilation: Ubuntu 16.04 server
  • Selenium execution: Linux Mint 18.1
fi11222
  • 275
  • 3
  • 11
  • Check the supported Chrome version of your chrome driver. I read that it supports Chrome 54 to 56 while your headless chrome is 59. Try to install a previous chrome version – BangTheBank Mar 05 '17 at 10:20
  • 3
    I can't. Because versions before 59 apparently did not support the headless mode. The feature dates back to mid 2016 only. Btw, how can I know what particular version of Chrome Webdriver supports ? – fi11222 Mar 05 '17 at 10:45
  • Also, I checked in the ChromeDriver Sources. There is apparently no maximum version, only a minimum, which is now 55 apparently. So the problem lies elsewhere. – fi11222 Mar 05 '17 at 10:55
  • https://sites.google.com/a/chromium.org/chromedriver/downloads It says Latest Release: ChromeDriver 2.27 Supports Chrome v54-56 When you work with Selenium you have to be careful about browsers versions and just use what current selenium drivers support. Usually, selenium drivers are a bit behind browsers. – BangTheBank Mar 05 '17 at 11:01
  • I cannot help you with this problem directly, but I wonder why you take the trouble to compile your own headless Chromium if there are other good headless browsers for Selenium. I recommend PhantomJS (based on WebKit engine) with GhostDriver, but for many cases HtmlUnit with activated JS is also good enough. BTW, if you always use the same engine for headless tests as for local tests with visible GUI, you become blind for cross-browser problems. – kriegaex Mar 05 '17 at 11:06
  • 1
    The reason I want to use Chromium is that it is the only natively headless browser which is also identical to a widely used production browser. For really pesky and complicated websites, HtmlUnit and PhantomJS just don't cut it. The scrolling, in particular, does not work like a normal browser and thus it creates issues with websites which use dynamic loading. It is possible to use an ordinary Firefox or Chrome with xfvb to make it "headless" but it is cumbersome and prone to break when upgrades are applied. Hence my wish to use Headless Chromium. – fi11222 Mar 05 '17 at 12:31
  • @BangTheBank: Again, in the sources, I do not find any trace of a **maximum** allowed version. They may say that it only works up to 56 because it is the maximum version they have tested it with but there is actually no blocking reason in the code why it would not work with a later version. The only issue here is the browser **name** probably, which ChromeDriver does not recognize and thus considers invalid. – fi11222 Mar 05 '17 at 12:35
  • @fi11222 Is the chrome driver launching the exception? If so, I've searched into code as well and it seems, as you guessed, that it fails because in browser identifier string there is no "Version/" string https://github.com/bayandin/chromedriver/blob/master/chrome/browser_info.cc#L107-L123 So, if you have access to headless chrome source code, try to identify where that "HeadlessChrome" is defined and change it to "HeadlessChrome Version" or something like that and rebuild. I think it might fix at least this exception. – BangTheBank Mar 05 '17 at 18:18
  • @fi11222 sorry ignore my last comment. Just look at this code https://github.com/bayandin/chromedriver/blob/master/chrome/browser_info.cc where the exception is actually launched and try to identify the problem. It's still something related to version strings or so. – BangTheBank Mar 05 '17 at 18:30

1 Answers1

13

ChromeHeadless is recognized by chromedriver since this patch (created after you have post your message), which is only available since chromedriver 2.29 (released in April 2017). Make sure that you have this chromedriver executable available in PATH and that Selenium is picking it instead of any other chromedriver you might have available.

Also, please note that - according to headless Chrome documentation - you need to pass two more flags:

l_option.add_argument('remote-debugging-port=9222')
l_option.add_argument('disable-gpu')

As for pre-built binaries of headless Chrome - that option is available since Chrome 57, so it is supported by all versions currently distributed through official Google repository (i.e. stable Chrome 58 and unstable Chrome 59). It is highlight of Chrome 59, so expect some rough edges until feature is stabilized.

Mirek Długosz
  • 4,205
  • 3
  • 24
  • 41
  • 1
    The additional options caused chrome to become unreachable, for me the only necessary options were `options.add_argument('headless')` and `options.add_argument('window-size=1904x950')`. – tarikki Jul 05 '17 at 07:57
  • 1
    I am using chromedriver 2.32 with the added the required flags but I still can't get headless to work. – Sebastian L Aug 11 '17 at 07:30
  • 1
    @SebastianL me too. I suspect it's because we're now using Chrome 60, but haven't figured out a fix yet. Did you manage to fix this? – goose Aug 11 '17 at 17:52
  • but I can see latest update 2.24.1 in my project pycharm. – ammy Sep 22 '17 at 10:10