Disable images in Selenium Google ChromeDriver

梦想的初衷 提交于 2019-12-18 19:05:14

问题


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

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

  • Chrome driver: V2.2
  • Chrome version: V29.0.1547.66 m
  • Selenium: V2.35

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

Attempt 1:

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

Attempt 2:

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

Attempt 3:

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

Attempt 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:

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

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

回答1:


This is my solution

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



回答2:


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

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).

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



回答3:


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:

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



回答4:


You should use --blink-settings instead of --disable-images by:

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#.




回答5:


I found out simple solution. This idea referred from 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);



回答6:


An easier approach would be solely:

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



回答7:


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.

See: this article



来源:https://stackoverflow.com/questions/18657976/disable-images-in-selenium-google-chromedriver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!