Java Selenium Chrome driver - Disable image loading

一笑奈何 提交于 2019-12-04 14:54:57

This should disable images for you.

    prefs.put("profile.managed_default_content_settings.images", 2); 

I found a small plugin that does a really good job

ChromeOptions op = new ChromeOptions();
    op.addExtensions(new File("C:\\whatever\\Block-image_v1.0.crx"));
    driver = new ChromeDriver(op);

if anyone else is interested, you can grab it here

Ireti

If you're running the headless mode, you can as well try
1.

 ChromeOptions options = new ChromeOptions();
 options.addArguments("--headless", "--disable-gpu", "--blink-settings=imagesEnabled=false");
 WebDriver driver = new ChromeDriver(options);
  1. Alternatively, you can create a new chrome profile. Disable images on by accessing chrome://settings/content on the new profile. Then add the new profile to your chromeDriver options. More info here https://startingwithseleniumwebdriver.blogspot.com/2015/07/working-with-chrome-profile-with.html

With Selenium 4 alpha 1, you can also use CDP for filtering URL's:

    ChromeOptions options = new ChromeOptions();

    ChromeDriver driver = new ChromeDriver(options);

    driver.getDevTools().createSession();

    driver.getDevTools().send(new Command<>("Network.enable", ImmutableMap.of()));
    driver.getDevTools().send(new Command<>("Network.setBlockedURLs", ImmutableMap.of("urls", ImmutableList.of("*://*/*.bmp","*://*/*.gif","*://*/*.png"))));

    driver.get("https://apache.org");

    driver.quit();

In the next Alpha version, the interface will be MUCH more user friendly.

Maven dependency:

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.0.0-alpha-1</version>
    </dependency>
vibi
public class Test {

    WebDriver driver;
    JavascriptExecutor jse;

    public void invokeChromeBrowser()
    {
        System.setProperty("webdriver.chrome.driver", "E:\\Softwares\\chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        disableChromeImages(options);
        driver = new ChromeDriver(options);

        driver.get("https://www.amazon.com/");

    }

    public static void disableChromeImages(ChromeOptions options)
    {
        HashMap<String, Object> images = new HashMap<String, Object>();
        images.put("images", 2);

        HashMap<String, Object> prefs = new HashMap<String, Object>();
        prefs.put("profile.default_content_setting_values", images);

        options.setExperimentalOption("prefs", prefs);

    }


    public static void main(String[] args) {

        Test Obj = new Test();
        Obj.invokeChromeBrowser();

    }

}

new ChromeDriver(DesiredCapabilities) is deprecated.

Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.managed_default_content_settings.images", 2); 
chromeOptions.setExperimentalOption("prefs", prefs);
Sethu_P

Check this code,

System.setProperty("webdriver.chrome.driver",
                    Settings.getProperty("ChromeDriverPath"));
DesiredCapabilities capabilities= DesiredCapabilities.chrome();

HashMap<String, Object> images = new HashMap<String, Object>(); 
images.put("images", 2);
HashMap<String, Object> prefs = new HashMap<String, Object>(); 
prefs.put("profile.default_content_setting_values", images);

ChromeOptions options= new ChromeOptions();
options.addArguments("--test-type --no-sandbox");
options.addArguments("--enable-strict-powerful-feature-restrictions");

options.setExperimentalOption("prefs", prefs); 
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(capabilities);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!