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
This should disable images for you.
prefs.put("profile.managed_default_content_settings.images", 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
Ireti
If you're running the headless mode, you can as well try
1.
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 on by accessing chrome://settings/content on the new profile. Then add the new profile to your chromeDriver options. More info here https://startingwithseleniumwebdriver.blogspot.com/2015/07/working-with-chrome-profile-with.html
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>
vibi
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();
}
}
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);
Sethu_P
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);
来源:https://stackoverflow.com/questions/35128850/java-selenium-chrome-driver-disable-image-loading