How to disable images in chrome using Selenium java?
Disabling images in Chrome during automated testing can enhance performance and speed up your Selenium tests. This is particularly useful when dealing with large web pages or when you want to focus on specific elements without the distraction of images.
In this guide, we'll walk you through how to disable images in Chrome using Selenium Java, ensuring that your test automation runs efficiently.
Prerequisites
- Java Development Kit (JDK) must be installed on your system.
- Eclipse IDE or any Java-compatible IDE.
- Basic knowledge of Java and Selenium WebDriver.
- Selenium WebDriver and Browser Driver installed.
Example
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class DisableImagesInChrome {
public static void main(String[] args) {
// Set up ChromeOptions to disable images
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized"); // Maximize the browser window
// Disable images using Chrome preferences
options.addArguments("profile.managed_default_content_settings.images=2"); // 2 disables images
// Set the path for the ChromeDriver executable
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver"); // Update the path to your ChromeDriver
// Initialize WebDriver with ChromeOptions
WebDriver driver = new ChromeDriver(options);
try {
// Navigate to the desired website
driver.get("https://www.geeksforgeeks.org/"); // Example URL
// Optional: Pause to observe the result
Thread.sleep(5000); // Pause for 5 seconds to see the result
} catch (InterruptedException e) {
// Handle interruption
e.printStackTrace();
} finally {
// Close the browser
driver.quit(); // Ensure the browser is closed
}
}
}
Output

Conclusion
Disabling images in Chrome using Selenium Java is a straightforward process that can significantly improve the speed of your test executions. By configuring ChromeOptions to disable images, you streamline the web page loading process, making your tests more efficient.
With the steps outlined above, you can easily implement this optimization in your Selenium tests, leading to faster and more focused automation.