Java Selenium Chrome驱动程序 - 禁用图像加载

时间:2016-02-01 11:00:48

标签: java google-chrome selenium selenium-chromedriver

我试图在没有加载任何图像的情况下运行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_settings", images); 

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

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

driver = new ChromeDriver(chromeCaps);

根本不起作用..

任何帮助都会受到很大关注

7 个答案:

答案 0 :(得分:3)

这应该为您禁用图像。

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

答案 1 :(得分:2)

我找到了一个做得非常好的小插件

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

如果其他人有兴趣,你可以抓住它here

答案 2 :(得分:2)

如果您正在运行无头模式,则也可以尝试
1。

 ChromeOptions options = new ChromeOptions();
 options.addArguments("--headless", "--disable-gpu", "--blink-settings=imagesEnabled=false");
 WebDriver driver = new ChromeDriver(options);
  1. 或者,您可以创建一个新的chrome配置文件。通过访问新配置文件上的chrome:// settings / content禁用图像。然后将新的配置文件添加到您的chromeDriver选项中。更多信息,请点击https://startingwithseleniumwebdriver.blogspot.com/2015/07/working-with-chrome-profile-with.html

答案 3 :(得分:0)

对于Selenium 4 alpha 1,您还可以使用CDP来过滤URL:

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

在下一个Alpha版本中,该界面将更加友好。

Maven依赖项:

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

答案 4 :(得分: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();

    }

}

答案 5 :(得分:0)

不推荐使用新的ChromeDriver(DesiredCapabilities)。

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

答案 6 :(得分:-1)

检查此代码,

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