3

I am trying to run chrome driver without loading any images for obvious reasons.

i found a piece of code online but i think it's outdated

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_settings", images); 

ChromeOptions options =new ChromeOptions(); 
options.setExperimentalOption("prefs", prefs); 

DesiredCapabilities chromeCaps = DesiredCapabilities.chrome(); 
chromeCaps.setCapability(ChromeOptions.CAPABILITY, options); 

driver = new ChromeDriver(chromeCaps);

does not work at all..

any help would be greatly appriciated

9 Answers 9

5

If you're running the headless mode, you can try

ChromeOptions options = new ChromeOptions();
options.addArguments("--headless", "--disable-gpu", "--blink-settings=imagesEnabled=false");
WebDriver driver = new ChromeDriver(options);

Alternatively, you can create a new chrome profile. Disable images by accessing chrome://settings/content on the new profile. Then add the new profile to your chromeDriver options. More info here.

Sign up to request clarification or add additional context in comments.

Comments

4

This should disable images for you.

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

Comments

4

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

Comments

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

1 Comment

There is a version 1.1 now available
0

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>

Comments

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

    }

}

1 Comment

Hi @user11709921. Welcome to Stackoverflow. A bit of an explanation about how this answer solves the problem, and where the OP erred in their code, would be great.
0

It worked for me.

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

Comments

0

Problem solved! It was in the old version of the selenium dependency

Solution:

<dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.0.0</version>
</dependency>


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
 
import java.util.HashMap;
import java.util.Map;
 
public class Test {
    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
 
        Map<String, Object> prefs = new HashMap<>();
        prefs.put("profile.managed_default_content_settings.images", 2);
 
        ChromeOptions options = new ChromeOptions();
        options.setExperimentalOption("prefs", prefs);
 
        WebDriver driver = new ChromeDriver(options);
 
        driver.get("https://yandex.ru/");
        Thread.sleep(15000);
 
        driver.quit();
    }
}

1 Comment

Code without any explanation are rarely helpful. Stack Overflow is about learning, not providing snippets to blindly copy and paste. Please edit your question and explain how it answers the specific question being asked. See How to Answer.
-1

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

2 Comments

Can you elaborate, what the initial problem was?
profile.default_content_setting_values - this worked

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.