Disable images in Selenium Google ChromeDriver.


We can disable images in Selenium in chromedriver. The images are sometimes disabled so that page load takes less time and execution is quick. In Chrome, we can do this with the help of the prefs setting.

Syntax

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

Let’s us make an attempt to disable all image from the below page −

Example

Code Implementation.

import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.util.HashMap;
import java.util.Map;
public class ChromeDisableImg {
   public static void main(String[] args) throws IOException {
      System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      Map<String, Object> prefs = new HashMap<String, Object>();
      // browser setting to disable image
      prefs.put("profile.managed_default_content_settings.images", 2);
      //adding capabilities to browser
      ChromeOptions op = new ChromeOptions();
      op.setExperimentalOption("prefs", prefs);
      // putting desired capabilities to browser
      WebDriver driver= new ChromeDriver(op);
      driver.get("https://www.tutorialspoint.com/index.htm/");
   }
}

Output

Updated on: 28-Dec-2020

970 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements