Java Selenium Chrome driver - Disable image loadin

2020-07-20 16:05发布

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

7条回答
走好不送
2楼-- · 2020-07-20 16:11
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();

    }

}
查看更多
冷血范
3楼-- · 2020-07-20 16:12

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>
查看更多
劳资没心,怎么记你
4楼-- · 2020-07-20 16:13

This should disable images for you.

    prefs.put("profile.managed_default_content_settings.images", 2); 
查看更多
你好瞎i
5楼-- · 2020-07-20 16:16

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);
查看更多
祖国的老花朵
6楼-- · 2020-07-20 16:18

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);
查看更多
闹够了就滚
7楼-- · 2020-07-20 16:22

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

查看更多
登录 后发表回答