禁用Selenium Google ChromeDriver中的图像

本文关键字:图像 ChromeDriver Selenium Google 禁用 | 更新日期: 2023-09-27 18:30:22

通过

Selenium和c#使用Google chrome时如何禁用图像?

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

  • 铬驱动程序:V2.2
  • 铬版本:V29.0.1547.66 m
  • 硒: 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);

禁用Selenium Google ChromeDriver中的图像

这是我

的解决方案

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

通过以下方式使用--blink-settings而不是--disable-images

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

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

driver.get_screenshot_as_file('headless.png')

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

对于您的方法 1-3,我没有看到此处列出的名为 --disable-images 的 Chrome 开关。因此,即使代码片段是正确的,它们无论如何都不会起作用。你从哪里得到这个开关?有什么参考资料吗?

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

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

使用 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);

更简单的方法只是:

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

我找到了简单的解决方案。这个想法来自Python:禁用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);

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

详见:本文