20

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);
Community
  • 1
  • 1
Fidel
  • 7,027
  • 11
  • 57
  • 81
  • 1
    Did you try the solution in that question? Also what version of ChromeDriver are you using? Please post the ways (the exact code) that you've tried and how it didn't work (if it simply didn't disable the images or if it threw an error). – Arran Sep 06 '13 at 13:45
  • possible duplicate of [Disable images in Selenium ChromeDriver](http://stackoverflow.com/questions/9433109/disable-images-in-selenium-chromedriver) – jww Sep 14 '14 at 00:19

7 Answers7

21

This is my solution

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

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

ryan
  • 847
  • 1
  • 10
  • 18
8

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);
}
Community
  • 1
  • 1
Yi Zeng
  • 32,020
  • 13
  • 97
  • 125
  • Thanks user, that worked well! The '--disable-images' argument came from a bunch of forum posts, evidently quite old. For example: http://productforums.google.com/forum/#!topic/chrome/wAbzmI0IoH8. – Fidel Sep 07 '13 at 10:24
  • Actually, that solution just prevents the images from displaying. Google chrome is still downloading them... – Fidel Sep 07 '13 at 10:26
  • 1
    @Fidel: Good to know it worked. So you actually want to prevent Chrome from downloading images, rather than disable images? – Yi Zeng Sep 07 '13 at 10:48
  • 1
    Yeah, I thought by saying 'disable' it wouldn't download them in the first place. – Fidel Sep 07 '13 at 10:54
8

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);
Fidel
  • 7,027
  • 11
  • 57
  • 81
  • 1
    Do you have a tip how to enable the plugin automatically? In my situation the button has to be clicked before the page is loaded!? – alfonx Sep 19 '13 at 01:37
  • Haha yeah I bumped into this problem too. I don't know how to turn it on automatically :/ I just put a Console.ReadKey() in my app to give me the chance to turn it on...pretty ghetto I know – Fidel Sep 19 '13 at 09:35
6

An easier approach would be solely:

ChromeOptions options = new ChromeOptions();
options.addArguments("headless","--blink-settings=imagesEnabled=false");
iamdanchiv
  • 4,052
  • 4
  • 37
  • 42
Xin Liu
  • 61
  • 1
  • 1
4

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);
Community
  • 1
  • 1
Jemin Lee
  • 41
  • 1
  • 1
    Thanks Jemin, looks great. Does it prevent the file being downloaded? Or does it prevent the file from being displayed? – Fidel Aug 17 '16 at 07:56
  • 1
    Your welcome Fidel. I didn't check about that topic yet. I just found that solution today. However I've checked loading time was less than before. I'll check later and tell you how it works. – Jemin Lee Aug 17 '16 at 17:29
  • 1
    Sorry to answer this just now, but i checked with fiddler and it indeed does NOT download images, great! – Joao Vitor Jun 15 '17 at 20:09
  • 2
    Do you know if it works with Chrome driver in headless mode? – rodrigorf Mar 20 '18 at 18:36
1

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

VMh
  • 1,300
  • 1
  • 13
  • 19