简体   繁体   中英

How to start Chrome without images using Watir?

I have tried this method but its not working .

 prefs = {
        :profile => {
            :managed_default_content_settings => { 
              :images => 2
            }
        }
    }

    Watir::Browser.new :chrome, :prefs => prefs
    browser.goto "http://www.example.com" 

I think it is like this

 profile = Selenium::WebDriver::Chrome::Profile.new
        profile['webkit.webprefs.loads_images_automatically'] = false

        @browser = Watir::Browser.new :chrome, :profile => profile

:prefs needs to be a key within the :options Hash:

browser = Watir::Browser.new :chrome, options: {prefs: prefs}

Specifically for disabling images:

browser = Watir::Browser.new(
  :chrome,
  options: {
    prefs: {'webkit.webprefs.loads_images_automatically' => false}
  }
)
browser.goto('www.google.com')
p browser.image.loaded?
#=> false

First, create a browser object like this:

require 'watir'
browser = Watir::Browser.new

This will open an empty Chrome window (assuming you have it installed) that you can control now.

browser.goto("google.com")

Chrome will navigate to this URL, and the page will load as if you typed the URL yourself.

Now: You can do anything you would normally do on a website with your mouse & keyboard.

Example:

browser.link(text: "All Posts").click

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM