一尘不染

在Selenium Google ChromeDriver中禁用图像

selenium

通过Selenium和c#使用图像时,如何禁用Google chrome中的图像?

我尝试了6种方法,但均无效果。

  • Chrome驱动程序:V2.2
  • Chrome版本:V29.0.1547.66 m
  • selenium:V2.35

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

尝试1:

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

尝试2:

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

尝试3:

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

尝试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);
}

尝试5:

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

尝试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);

阅读 565

收藏
2020-06-26

共1个答案

一尘不染

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

    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);
2020-06-26