Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable images in Selenium Google ChromeDriver

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);
like image 980
Fidel Avatar asked Sep 06 '13 12:09

Fidel


People also ask

How do I disable ChromeDriver?

browser. close() will close only the current chrome window. browser. quit() should close all of the open windows, then exit webdriver.

What is Webdriver ChromeOptions ()?

ChromeOptions is a new concept added in Selenium WebDriver starting from Selenium version 3.6. 0 which is used for customizing the ChromeDriver session. By default when selenium opens up any browser (Chrome browser or Firefox browser), it opens up without any extension or history or cookies, etc.


3 Answers

This is my solution

IWebDriver driver;
ChromeOptions options = new ChromeOptions();
options.AddUserProfilePreference("profile.default_content_setting_values.images", 2);
driver = new ChromeDriver(options);
like image 132
Phuc Nguyen Minh Avatar answered Oct 18 '22 22:10

Phuc Nguyen Minh


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

like image 27
ryan Avatar answered Oct 18 '22 22:10

ryan


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);
}
like image 35
Yi Zeng Avatar answered Oct 19 '22 00:10

Yi Zeng