Disable images in Selenium Google ChromeDriver

后端 未结 7 1344
悲哀的现实
悲哀的现实 2020-12-16 13:58

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 Stack

相关标签:
7条回答
  • 2020-12-16 14:18

    An easier approach would be solely:

    ChromeOptions options = new ChromeOptions();
    options.addArguments("headless","--blink-settings=imagesEnabled=false");
    
    0 讨论(0)
  • 2020-12-16 14:20

    This is my solution

    IWebDriver driver;
    ChromeOptions options = new ChromeOptions();
    options.AddUserProfilePreference("profile.default_content_setting_values.images", 2);
    driver = new ChromeDriver(options);
    
    0 讨论(0)
  • 2020-12-16 14:28

    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);
    
    0 讨论(0)
  • 2020-12-16 14:42

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

    0 讨论(0)
  • 2020-12-16 14:43

    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

    0 讨论(0)
  • 2020-12-16 14:44

    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);
    }
    
    0 讨论(0)
提交回复
热议问题