C# 在 Selenium Google ChromeDriver 中禁用图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18657976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:52:16  来源:igfitidea点击:

Disable images in Selenium Google ChromeDriver

c#seleniumwebdriverselenium-webdriverselenium-chromedriver

提问by Fidel

How does one disable images in Google chrome when using it through Selenium and c#?

当通过 Selenium 和 C# 使用 Google chrome 时,如何禁用它?

I've attempted 6 ways and none worked. I've even tried the answer on thisStackOverflow question, however I think the info in it is out of date.

我尝试了 6 种方法,但都没有奏效。我什至尝试过这个StackOverflow 问题的答案,但是我认为其中的信息已经过时。

  • Chrome driver: V2.2
  • Chrome version: V29.0.1547.66 m
  • Selenium: V2.35
  • Chrome 驱动程序:V2.2
  • Chrome 版本:V29.0.1547.66 m
  • 硒:V2.35

All the attempts I've made don't cause exceptions, they run normally but still display images:

我所做的所有尝试都不会导致异常,它们运行正常但仍显示图像:

Attempt 1:

尝试 1:

ChromeOptions co = new ChromeOptions();
co.AddArgument("--disable-images");
IWebDriver driver = new ChromeDriver(co);

Attempt 2:

尝试 2:

DesiredCapabilities capabilities = DesiredCapabilities.Chrome();
capabilities.SetCapability("chrome.switches", new string[1] { "disable-images" });

Attempt 3:

尝试 3:

ChromeOptions co = new ChromeOptions();
co.AddAdditionalCapability("chrome.switches", new string[1] { "disable-images" });

Attempt 4:

尝试 4:

var imageSetting = new Dictionary<string, object>();
imageSetting.Add("images", 2);
Dictionary<string, object> content = new Dictionary<string, object>();
content.Add("profile.default_content_settings", imageSetting);
var prefs = new Dictionary<string, object>();
prefs.Add("prefs", content);
var options = new ChromeOptions();
var field = options.GetType().GetField("additionalCapabilities", BindingFlags.Instance | BindingFlags.NonPublic);
if (field != null)
{
    var dict = field.GetValue(options) as IDictionary<string, object>;
    if (dict != null)
        dict.Add(ChromeOptions.Capability, prefs);
}

Attempt 5:

尝试 5:

ChromeOptions options = new ChromeOptions();
options.AddAdditionalCapability("profile.default_content_settings", 2);

Attempt 6:

尝试 6:

Dictionary<String, Object> contentSettings = new Dictionary<String, Object>();
contentSettings.Add("images", 2);
Dictionary<String, Object> preferences = new Dictionary<String, Object>();
preferences.Add("profile.default_content_settings", contentSettings);
DesiredCapabilities caps = DesiredCapabilities.Chrome();
caps.SetCapability("chrome.prefs", preferences);

采纳答案by Fidel

Use http://chrome-extension-downloader.com/to download the "Block Image" extension (https://chrome.google.com/webstore/detail/block-image/pehaalcefcjfccdpbckoablngfkfgfgj?hl=en-GB). The extension prevents the image from being downloaded in the first place. Now it's just a matter of loading it using the following statement:

使用http://chrome-extension-downloader.com/下载“阻止图像”扩展程序 ( https://chrome.google.com/webstore/detail/block-image/pehaalcefcjfccdpbckoablngfkfgfgj?hl=en-GB)。该扩展程序首先阻止下载图像。现在只需使用以下语句加载它:

    var options = new ChromeOptions();

    //use the block image extension to prevent images from downloading.
    options.AddExtension("Block-image_v1.0.crx");

    var driver = new ChromeDriver(options);

回答by Yi Zeng

For your method 1-3, I don't see a Chrome switch called --disable-imageslisted here. So even if the code snippets are correct, they won't work no matter what. Where did you get that switch? Any references?

对于您的方法 1-3,我没有看到名为--disable-images此处列出的 Chrome 开关。因此,即使代码片段是正确的,它们无论如何也无法工作。你从哪里弄到的那个开关?有参考吗?

For the methods 4-6, I assume you got the idea from this chromdriver issue. I don't know if this {'profile.default_content_settings': {'images': 2}}is still valid or not, but you can give it a try with the following code (which was originally the answer to How to set Chrome preferences using Selenium Webdriver .NET binding?, answer provided by Martin Devillers).

对于方法 4-6,我假设您从这个 chromdriver issue得到了这个想法。我不知道这{'profile.default_content_settings': {'images': 2}}是否仍然有效,但是您可以使用以下代码尝试一下(最初是如何使用 Selenium Webdriver .NET 绑定设置 Chrome 首选项的答案Martin Devellers提供的答案) .

public class ChromeOptionsWithPrefs: ChromeOptions {
    public Dictionary<string,object> prefs { get; set; }
}

public static void Initialize() {
    var options = new ChromeOptionsWithPrefs();
    options.prefs = new Dictionary<string, object> {
        { "profile.default_content_settings", new Dictionary<string, object>() { "images", 2 } }
    };
    var driver = new ChromeDriver(options);
}

回答by VMh

I recommend that you create a new profile, customize this profile so that it does not load the images, and use this profile as a chromeOption to set up your driver.

我建议您创建一个新的配置文件,自定义此配置文件以使其不加载图像,并将此配置文件用作 chromeOption 来设置您的驱动程序。

See: this article

见:这篇文章

回答by Jemin Lee

I found out simple solution. This idea referred from Python:Disable images in Selenium Google ChromeDriver

我找到了简单的解决方案。这个想法来自Python:Disable images in Selenium Google ChromeDriver

var service = ChromeDriverService.CreateDefaultService(@WebDriverPath);

var options = net ChromeOptions();
options.AddUserProfilePreference("profile.managed_default_content_settings.images", 2);
IWebDriver Driver = new ChromeDriver(service, options);

回答by Phuc Nguyen Minh

This is my solution

这是我的解决方案

IWebDriver driver;
ChromeOptions options = new ChromeOptions();
options.AddUserProfilePreference("profile.default_content_setting_values.images", 2);
driver = new ChromeDriver(options);

回答by ryan

You should use --blink-settingsinstead of --disable-imagesby:

您应该使用--blink-settings代替--disable-images

options.add_argument('--blink-settings=imagesEnabled=false')

which also works in headless mode. Setting profile doesn't work in headless mode. You can verify it by screenshot:

这也适用于无头模式。设置配置文件在无头模式下不起作用。你可以通过截图来验证:

driver.get_screenshot_as_file('headless.png')

Note: I was using python with selenium, but I think it should be easy to transfer to c#.

注意:我使用 python 和 selenium,但我认为它应该很容易转移到 c#。

回答by Xin Liu

An easier approach would be solely:

一种更简单的方法是:

ChromeOptions options = new ChromeOptions();
options.addArguments("headless","--blink-settings=imagesEnabled=false");