Re-enable Images in Chrome WebDriver After Selenium
In this article
Re-enabling images in Chrome WebDriver after disabling them with Selenium restores image loading in automated browser sessions. This is useful when you initially disabled images to speed up page loads or reduce bandwidth, but now need images visible for testing, scraping, or interaction. Re-enabling ensures accurate visual rendering and proper element detection. The process involves modifying ChromeOptions to remove or override previous image-blocking preferences and restarting the WebDriver session with the updated settings.
What You'll Need
- Selenium WebDriver for Python (or your preferred language)
- ChromeDriver compatible with your Chrome browser version
- Basic knowledge of ChromeOptions and WebDriver configuration
- Access to the script where images were originally disabled
Step-by-Step
- Open your Selenium script where images were previously disabled using ChromeOptions. Locate the section where
chrome_optionsoroptionsis defined. - Find the line that disables images. It typically looks like:
This setting tells Chrome to block images (value 2). To re-enable, you must either remove this line or change the value to 1 (allow images).prefs = {"profile.managed_default_content_settings.images": 2} - Modify the preference to allow images by changing the value from 2 to 1:
Alternatively, if no other preferences are needed, you can remove the entire prefs block.prefs = {"profile.managed_default_content_settings.images": 1} - If you're using
add_experimental_option("prefs", prefs), ensure the updated prefs dictionary is passed correctly. Example:
This configures Chrome to load images in the new session.from selenium import webdriver options = webdriver.ChromeOptions() prefs = {"profile.managed_default_content_settings.images": 1} options.add_experimental_option("prefs", prefs) driver = webdriver.Chrome(options=options) - If you had disabled images globally via command-line arguments (rare), remove any such flags like
--disable-imagesor--blink-settings=imagesEnabled=falsefrom your options. - Quit any existing WebDriver instance before starting a new one. Use
driver.quit()to close all browser windows and terminate the session cleanly. - Launch a new WebDriver instance with the updated options. The browser should now load images on all visited pages.
- Test the configuration by navigating to a page with visible images (e.g., a news site or image gallery). Confirm images are loading as expected.
- Optionally, verify the setting in Chrome by visiting
chrome://settings/content/imagesin the WebDriver-controlled browser. It should show "Sites can show images". - For dynamic control, consider creating two separate option sets—one for image-off and one for image-on—and switch between them as needed in your automation flow.
Common Mistakes
- Forgetting to quit the old driver: If you don’t call
driver.quit(), the new settings won’t take effect because Chrome retains session-level preferences. - Not updating the prefs dictionary: Leaving the image setting at 2 or not including the updated prefs in
add_experimental_optionmeans images remain blocked. - Using incorrect preference keys: The correct key is
profile.managed_default_content_settings.images. Variants likedefault_content_settingsorcontent_settingsalone won’t work. - Assuming images re-enable automatically: Chrome does not reset content settings between sessions unless explicitly configured. You must reapply the desired settings each time.
- Overlooking multiple preference blocks: If your script applies prefs in multiple places, ensure all are updated to avoid conflicts.
Tips
- Use a configuration variable for image settings to toggle easily:
load_images = True image_setting = 1 if load_images else 2 - Combine image settings with other content preferences (e.g., JavaScript, cookies) for fine-grained control.
- Log the current prefs in your script during development to confirm they’re applied correctly.
- Use headless mode with images enabled for faster visual testing in CI/CD pipelines.
- Always test on a live site with diverse image types (JPEG, PNG, WebP) to ensure compatibility.
Troubleshooting
If images still don’t load after re-enabling:
- Check Chrome version compatibility: Ensure ChromeDriver matches your Chrome browser version. Mismatches can cause preferences to be ignored.
- Verify prefs are applied: Add a print statement or log to confirm the prefs dictionary contains
"images": 1. - Inspect chrome://settings: In the WebDriver browser, navigate to
chrome://settings/content/images. If it shows "Blocked", the setting wasn’t applied. - Look for conflicting options: Other experimental prefs or command-line flags might override image settings. Simplify options to isolate the issue.
- Test in non-headless mode: Run without
--headlessto visually confirm images appear. - Restart the entire script: Sometimes lingering processes interfere. Close all Chrome instances and rerun.
- Use a fresh user profile: Add
options.add_argument("--user-data-dir=/tmp/chrome-profile")to avoid cached settings.
Frequently Asked Questions
Does re-enabling images affect performance?
Yes, loading images increases page load time and memory usage. Only enable when necessary for accurate testing or data extraction.
Can I toggle images without restarting the driver?
No. Chrome content settings are applied at session start. You must quit and restart the driver with new options.
Why are some images still not loading?
Some sites use lazy loading or JavaScript to load images. Ensure JavaScript is enabled and consider waiting with WebDriverWait for dynamic content.
Is this method the same for other browsers?
No. Firefox and Safari use different preference keys and APIs. This guide applies only to Chrome WebDriver.
Can I enable images for specific sites only?
Not directly through Selenium. Chrome’s content settings allow only global image rules. Site-specific rules require extensions or manual configuration.
Do I need admin rights to change these settings?
No. ChromeOptions are user-level settings and do not require elevated privileges.