Selenium: Chrome ignores preferences if headless flag is set

Created on 10 Aug 2017  ·  9Comments  ·  Source: SeleniumHQ/selenium

Meta -

OS: macOS 10.12

Selenium Version: Ruby selenium-webdriver (3.4.4)

Browser: Chrome

Browser Version: Chrome 60.0.3112.90 / Canary 62.0.3181.0

Expected Behavior -

I want to be able to configure preferences when in headless mode. Something like this:

Capybara.register_driver :headless_chrome do |app|
  Capybara::Selenium::Driver.new(
    app,
    browser: :chrome,
    options: Selenium::WebDriver::Chrome::Options.new(
    args: %w[headless disable-gpu],
    prefs: {
      "profile.managed_default_content_settings.images" => 2
    }
  )
)
end

However it does attempt to load images so the prefs setting isn't sticking.

But if I remove headless then the preference works. So headless mode doesn't seem to work with preferences.

I also tried using desired_capabilities and/or profile instead of options but couldn't get it to stick that way either.

Steps to reproduce -

It seems likely this is a Chrome issue but as I'm using it via Selenium I'm posting here.

D-chrome

Most helpful comment

For anyone who comes across this post like I did here is how to disable images using Selenium with Chrome Headless.

options = Selenium::WebDriver::Chrome::Options.new(args: ['headless', '--blink-settings=imagesEnabled=false'])

@driver = Selenium::WebDriver.for(:chrome, options: options)

All 9 comments

Can you try it without using Capybara, and setting Selenium::WebDriver.logger.level = :debug before instantiating the browser? After doing so, please post the trace so I can see the payload being sent over

So many layers. Okay, I've created a minimal reproduction using just selenium-webdriver. There's a README and comments explaining how to reproduce: https://github.com/sfcgeorge/selenium-chrome-prefs-bug

The logs look the same except for a sneaky malloc error 🤔

Thanks 😄


Debug: Normal (working fine).

2017-08-11 11:07:47 DEBUG Selenium Executing Process ["/usr/local/bin/chromedriver", "--port=9515"]
2017-08-11 11:07:47 DEBUG Selenium polling for socket on ["127.0.0.1", 9515]
Starting ChromeDriver 2.31.488774 (7e15618d1bf16df8bf0ecf2914ed1964a387ba0b) on port 9515
Only local connections are allowed.
2017-08-11 11:07:47 INFO Selenium -> POST session
2017-08-11 11:07:47 INFO Selenium    >>> http://127.0.0.1:9515/session | {"desiredCapabilities":{"browserName":"chrome","version":"","platform":"ANY","javascriptEnabled":true,"cssSelectorsEnabled":true,"takesScreenshot":false,"nativeEvents":false,"rotatable":false,"chromeOptions":{"args":["disable-gpu"],"prefs":{"profile.managed_default_content_settings.images":2}}},"capabilities":{"firstMatch":[{"browserName":"chrome"}]}}
2017-08-11 11:07:47 DEBUG Selenium      > {"Accept"=>"application/json", "Content-Type"=>"application/json; charset=utf-8", "Content-Length"=>"353"}
2017-08-11 11:07:48 INFO Selenium <- {"sessionId":"ff3b6e84efebae3ca24a07b031b79c5e","status":0,"value":{"acceptSslCerts":true,"applicationCacheEnabled":false,"browserConnectionEnabled":false,"browserName":"chrome","chrome":{"chromedriverVersion":"2.31.488774 (7e15618d1bf16df8bf0ecf2914ed1964a387ba0b)","userDataDir":"/var/folders/48/tq7k6jzd3k12q11n978s09qc0000gn/T/.org.chromium.Chromium.19PG3w"},"cssSelectorsEnabled":true,"databaseEnabled":false,"handlesAlerts":true,"hasTouchScreen":false,"javascriptEnabled":true,"locationContextEnabled":true,"mobileEmulationEnabled":false,"nativeEvents":true,"networkConnectionEnabled":false,"pageLoadStrategy":"normal","platform":"Mac OS X","rotatable":false,"setWindowRect":true,"takesHeapSnapshot":true,"takesScreenshot":true,"unexpectedAlertBehaviour":"","version":"60.0.3112.90","webStorageEnabled":true}}
2017-08-11 11:07:48 INFO Selenium Detected OSS dialect.
file:///Users/sfcgeorge/Documents/Projects/Coding/Ruby/selenium-chrome-prefs/test.html
2017-08-11 11:07:48 INFO Selenium -> POST session/ff3b6e84efebae3ca24a07b031b79c5e/url
2017-08-11 11:07:48 INFO Selenium    >>> http://127.0.0.1:9515/session/ff3b6e84efebae3ca24a07b031b79c5e/url | {"url":"file:///Users/sfcgeorge/Documents/Projects/Coding/Ruby/selenium-chrome-prefs/test.html"}
2017-08-11 11:07:48 DEBUG Selenium      > {"Accept"=>"application/json", "Content-Type"=>"application/json; charset=utf-8", "Content-Length"=>"96"}
2017-08-11 11:07:48 INFO Selenium <- {"sessionId":"ff3b6e84efebae3ca24a07b031b79c5e","status":0,"value":null}


Debug: Headless (pref ignored bug). Note malloc error ¯⁠_(ツ)_/⁠¯

2017-08-11 11:06:43 DEBUG Selenium Executing Process ["/usr/local/bin/chromedriver", "--port=9515"]
2017-08-11 11:06:43 DEBUG Selenium polling for socket on ["127.0.0.1", 9515]
Starting ChromeDriver 2.31.488774 (7e15618d1bf16df8bf0ecf2914ed1964a387ba0b) on port 9515
Only local connections are allowed.
2017-08-11 11:06:44 INFO Selenium -> POST session
2017-08-11 11:06:44 INFO Selenium    >>> http://127.0.0.1:9515/session | {"desiredCapabilities":{"browserName":"chrome","version":"","platform":"ANY","javascriptEnabled":true,"cssSelectorsEnabled":true,"takesScreenshot":false,"nativeEvents":false,"rotatable":false,"chromeOptions":{"args":["headless","disable-gpu"],"prefs":{"profile.managed_default_content_settings.images":2}}},"capabilities":{"firstMatch":[{"browserName":"chrome"}]}}
2017-08-11 11:06:44 DEBUG Selenium      > {"Accept"=>"application/json", "Content-Type"=>"application/json; charset=utf-8", "Content-Length"=>"364"}
2017-08-11 11:06:44 INFO Selenium <- {"sessionId":"fd9e03ba6ee237f7e56a26b85b05b49e","status":0,"value":{"acceptSslCerts":true,"applicationCacheEnabled":false,"browserConnectionEnabled":false,"browserName":"chrome","chrome":{"chromedriverVersion":"2.31.488774 (7e15618d1bf16df8bf0ecf2914ed1964a387ba0b)","userDataDir":"/var/folders/48/tq7k6jzd3k12q11n978s09qc0000gn/T/.org.chromium.Chromium.14uAiD"},"cssSelectorsEnabled":true,"databaseEnabled":false,"handlesAlerts":true,"hasTouchScreen":false,"javascriptEnabled":true,"locationContextEnabled":true,"mobileEmulationEnabled":false,"nativeEvents":true,"networkConnectionEnabled":false,"pageLoadStrategy":"normal","platform":"Mac OS X","rotatable":false,"setWindowRect":true,"takesHeapSnapshot":true,"takesScreenshot":true,"unexpectedAlertBehaviour":"","version":"60.0.3112.90","webStorageEnabled":true}}
2017-08-11 11:06:44 INFO Selenium Detected OSS dialect.
file:///Users/sfcgeorge/Documents/Projects/Coding/Ruby/selenium-chrome-prefs/test.html
2017-08-11 11:06:44 INFO Selenium -> POST session/fd9e03ba6ee237f7e56a26b85b05b49e/url
2017-08-11 11:06:44 INFO Selenium    >>> http://127.0.0.1:9515/session/fd9e03ba6ee237f7e56a26b85b05b49e/url | {"url":"file:///Users/sfcgeorge/Documents/Projects/Coding/Ruby/selenium-chrome-prefs/test.html"}
2017-08-11 11:06:44 DEBUG Selenium      > {"Accept"=>"application/json", "Content-Type"=>"application/json; charset=utf-8", "Content-Length"=>"96"}
2017-08-11 11:06:44 INFO Selenium <- {"sessionId":"fd9e03ba6ee237f7e56a26b85b05b49e","status":0,"value":null}
chromedriver(48860,0x70000fcfd000) malloc: *** error for object 0x7fd2e7e04310: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug

I'm able to reproduce the same issue without selenium, just using chrome-remote-interface

Great, thanks for reproducing. I guess the bug is in Chromedriver then? I've opened an issue there: https://bugs.chromium.org/p/chromedriver/issues/detail?id=1925

Feel free to comment there with any useful information, I'm not really sure what to look for.

Thanks @sweinertjr for investigating that!

For anyone who comes across this post like I did here is how to disable images using Selenium with Chrome Headless.

options = Selenium::WebDriver::Chrome::Options.new(args: ['headless', '--blink-settings=imagesEnabled=false'])

@driver = Selenium::WebDriver.for(:chrome, options: options)

@akaDanPaul is this going to stop the downloading or it just prevents the image from displaying?

@CharlesCCC When I tested this with Selenium / Capybara in a rails project, I could tell by my server logs that images were not downloaded (rather than simply hidden).

Was this page helpful?
0 / 5 - 0 ratings