cubesnyc
cubesnyc

Reputation: 1545

ChromeDriver Ruby disable images

Is there a way to disable images in Chromedriver with ruby? There is a similar question but it deals with C# and I am not exactly sure how to port it over to ruby.

Disable images in Selenium ChromeDriver

Upvotes: 3

Views: 1917

Answers (4)

Dorian
Dorian

Reputation: 23989

Disabling notifications and images:

Capybara.register_driver :selenium_chrome do |app|
  prefs = { "profile.managed_default_content_settings.notifications" => 2 }

  caps = Selenium::WebDriver::Remote::Capabilities.chrome(chrome_options: { prefs: prefs })

  profile = Selenium::WebDriver::Chrome::Profile.new
  profile["profile.default_content_settings"] = { :images => '2' }

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

  Capybara::Selenium::Driver.new(
    app,
    browser: :chrome,
    desired_capabilities: caps,
    profile: profile,
    options: options
  )
end

Upvotes: 0

akaDanPaul
akaDanPaul

Reputation: 283

For anyone who comes across this and is using chrome headless, here is how to disable images.

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

Upvotes: 4

Raj
Raj

Reputation: 3061

The flag to be set has changed since @bbbco added his answer. The correct flag is: "profile.managed_default_content_settings.images" making the working code:

profile = Selenium::WebDriver::Chrome::Profile.new
profile["profile.managed_default_content_settings.images"] = 2

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

Upvotes: 0

bbbco
bbbco

Reputation: 1540

Looks like it requires a hash to be sent to the "profile.default_content_settings" Chrome profile setting. I would try something like this:

profile = Selenium::WebDriver::Chrome::Profile.new
profile["profile.default_content_settings"] = { :images => '2' }

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

Upvotes: 0

Related Questions