在Selenium Google ChromeDriver中停用图片

Fid*_*del 14 c# selenium webdriver selenium-chromedriver selenium-webdriver

如何通过Selenium和c#使用Google Chrome中的图像?

我尝试过6种方法而且都没有用.我甚至尝试过这个 StackOverflow问题的答案,但我认为其中的信息已经过时了.

  • Chrome驱动程序:V2.2
  • Chrome版本:V29.0.1547.66米
  • 硒:V2.35

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

尝试1:

ChromeOptions co = new ChromeOptions();
co.AddArgument("--disable-images");
IWebDriver driver = new ChromeDriver(co);
Run Code Online (Sandbox Code Playgroud)

尝试2:

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

尝试3:

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

尝试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);
}
Run Code Online (Sandbox Code Playgroud)

尝试5:

ChromeOptions options = new ChromeOptions();
options.AddAdditionalCapability("profile.default_content_settings", 2);
Run Code Online (Sandbox Code Playgroud)

尝试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);
Run Code Online (Sandbox Code Playgroud)

小智 14

这是我的解决方案

IWebDriver driver;
ChromeOptions options = new ChromeOptions();
options.AddUserProfilePreference("profile.default_content_setting_values.images", 2);
driver = new ChromeDriver(options);
Run Code Online (Sandbox Code Playgroud)


Yi *_*eng 8

对于方法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);
}
Run Code Online (Sandbox Code Playgroud)


Fid*_*del 8

使用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);
Run Code Online (Sandbox Code Playgroud)

  • 您有如何自动启用该插件的提示吗?在我的情况下,必须在加载页面之前单击按钮!? (2认同)

rya*_*yan 7

您应该使用--blink-settings而不是--disable-images

options.add_argument('--blink-settings=imagesEnabled=false')
Run Code Online (Sandbox Code Playgroud)

在无头模式下也可以使用。设置个人资料在无头模式下不起作用。您可以通过屏幕截图进行验证:

driver.get_screenshot_as_file('headless.png')
Run Code Online (Sandbox Code Playgroud)

注意:我曾经将python和硒一起使用,但是我认为它应该很容易转移到c#中。

  • 注意:它只是不显示图像。它仍然可以获取它们。 (2认同)

小智 6

一种更简单的方法是:

ChromeOptions options = new ChromeOptions();
options.addArguments("headless","--blink-settings=imagesEnabled=false");
Run Code Online (Sandbox Code Playgroud)