我试图在没有加载任何图像的情况下运行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);
根本不起作用..
任何帮助都会受到很大关注
答案 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);
答案 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);