简体   繁体   English

如何使用 Watir 在没有图像的情况下启动 Chrome?

[英]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: :prefs需要是:options哈希中的一个键:

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.这将打开一个您现在可以控制的空 Chrome 窗口(假设您已安装它)。

browser.goto("google.com")

Chrome will navigate to this URL, and the page will load as if you typed the URL yourself. Chrome 将导航到此 URL,并且页面将加载,就像您自己键入 URL 一样。

Now: You can do anything you would normally do on a website with your mouse & keyboard.现在:您可以使用鼠标和键盘执行通常在网站上执行的任何操作。

Example:例子:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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