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

Java Selenium Chrome driver - Disable image loading

出于显而易见的原因,我正在尝试 运行 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);

根本不起作用..

任何帮助将不胜感激

这应该会为您禁用图像。

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

我发现了一个非常有用的小插件

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

大家有兴趣可以抢here

检查此代码,

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

如果你是运行无头模式,你可以试试

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

或者,您可以创建一个新的 chrome 配置文件。通过访问新个人资料上的 chrome://settings/content 来禁用图像。然后将新的配置文件添加到您的 chrome 驱动程序选项中。更多信息 here.

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

    }

}

新的 ChromeDriver(DesiredCapabilities) 已弃用。

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

对我有用。

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

问题已解决! 它在旧版本的 selenium 依赖中

解决方案:

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