User Agents

Web browsers and everyday web tech, explained

Disable Images in Chrome for Selenium Screenshots

· 5 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Disabling images in Chrome while taking Selenium screenshots makes pages load faster, reduces bandwidth, and produces cleaner, more consistent screenshots—especially useful for layout testing or when visual content isn't relevant. You can do this by launching Chrome with a custom profile that blocks image requests, or by using Chrome DevTools Protocol (CDP) to disable images on the fly. This guide covers both methods, plus a simple JavaScript trick for pages that load images via CSS.

What You'll Need

  • Python 3.x (or Java/JavaScript—the logic is the same)
  • Selenium WebDriver installed (pip install selenium)
  • Chrome browser and matching ChromeDriver (or use Selenium Manager)
  • Basic familiarity with writing Selenium scripts

Step-by-Step

  1. Set up your Selenium project. Create a new Python file (e.g., screenshot_no_images.py) and import the necessary modules: from selenium import webdriver and from selenium.webdriver.chrome.options import Options.
  2. Create Chrome options with a custom profile. The most reliable way is to launch Chrome with a user-data directory that has images disabled. In your code, create an Options object and add the argument --user-data-dir=/path/to/custom/profile. Use a fresh directory each time to avoid profile corruption.
  3. Pre-configure the profile to block images. Before launching Chrome, create the profile directory and write a Preferences file inside it. The file should contain JSON that sets profile.default_content_setting_values.images to 2 (which means "block"). For example:
    {
      "profile": {
        "default_content_setting_values": {
          "images": 2
        }
      }
    }
    Save this as Preferences in the profile directory (e.g., /tmp/chrome-profile/Preferences).
  4. Launch Chrome with those options. In your script, do:
    options = Options()
    options.add_argument("--user-data-dir=/tmp/chrome-profile")
    driver = webdriver.Chrome(options=options)
    This tells Chrome to use that profile, which has images blocked.
  5. Alternatively, use Chrome DevTools Protocol (CDP). If you prefer not to manage a profile, you can disable images for the current session using CDP. After creating the driver, run:
    driver.execute_cdp_cmd("Network.setBlockedURLs", {"urls": ["*.png", "*.jpg", "*.jpeg", "*.gif", "*.webp", "*.svg"]})
    driver.execute_cdp_cmd("Network.enable", {})
    This blocks common image file extensions. Note: it won't block images served as data URLs or via CSS backgrounds.
  6. For CSS background images, use a JavaScript override. If the page uses CSS backgrounds, add a script that sets all elements' background-image to none before taking the screenshot:
    driver.execute_script("""
      var all = document.querySelectorAll('*');
      for (var i = 0; i < all.length; i++) {
        all[i].style.backgroundImage = 'none';
      }
    """)
    Run this after the page loads, right before capturing the screenshot.
  7. Take the screenshot. Use driver.save_screenshot('output.png') or driver.get_screenshot_as_file('output.png'). The image will be captured without any images loaded.
  8. Clean up. Always call driver.quit() in a finally block to close the browser and release resources.

Common Mistakes

  • Using a profile that already has images enabled. If you reuse a profile directory, the Preferences file might be overwritten by Chrome. Always use a fresh directory or re-write the Preferences file before each run.
  • Blocking only image extensions via CDP. Many sites inline images as base64 data URLs—these won't match the URL patterns. Use the profile method for full coverage.
  • Forgetting to call Network.enable after setBlockedURLs. Without it, the blocking rules may not take effect.
  • Applying the JavaScript background-image override too early. If the page is still loading, elements might change. Wait for the page to be fully loaded (or use explicit waits) before applying the override.

Tips

  • Combine methods for maximum coverage. Use the profile method to block <img> tags and the JavaScript override for CSS backgrounds. This covers nearly all image types.
  • Use a unique profile directory per test run. This avoids stale settings and ensures a clean state. You can use Python's tempfile.mkdtemp() to create a temporary directory.
  • For headless mode, the same profile method works. Just add --headless=new to the options.
  • If you only need to disable images for a few screenshots, consider using CDP. It's session-specific and doesn't require file management.
  • Test with a simple page first. Create a local HTML file with an image and verify your method works before applying it to complex sites.

Troubleshooting

  • Images still appear. Check that the profile Preferences file is correctly formatted and that the path is correct. Also, verify that Chrome is actually using that profile by checking the browser's settings (e.g., go to chrome://settings/content/images—it should show "Blocked").
  • Chrome fails to launch with the custom profile. This can happen if the profile directory is locked or has permission issues. Delete the directory and recreate it, or use a different path.
  • CDP method blocks images but the page layout shifts. That's expected because images take up space. If you need the layout to remain the same, you might want to use a different approach, like loading images but hiding them with CSS (e.g., opacity: 0), but that still downloads them.
  • Screenshots are blank or black. This usually means the page didn't load fully. Add an explicit wait for the page to reach a ready state before taking the screenshot.
  • JavaScript override doesn't affect lazy-loaded images. Some images load on scroll. Use Selenium to scroll the page first, then apply the override, then take the screenshot.

Frequently Asked Questions

Will disabling images speed up my Selenium tests?

Yes, typically. Fewer network requests mean faster page loads, especially on image-heavy sites. The improvement is more noticeable on slower connections.

Can I disable images only for one screenshot and keep them for others?

Yes. With the CDP method, you can call Network.setBlockedURLs with an empty list to re-enable images later in the same session. With the profile method, you'd need to restart the browser without the profile.

Does this work in headless mode?

Yes, both the profile and CDP methods work in headless Chrome. Just add the appropriate headless argument.

Will this affect JavaScript or CSS?

No, disabling images only blocks image requests. JavaScript and CSS still load and execute normally.

Can I block only certain images, like ads?

Yes. With CDP, you can specify patterns like *.adserver.com/* or *.png to block selectively.

Is there a way to do this without writing a profile file?

Yes, you can use the CDP method, or you can use a Chrome extension that blocks images, but that adds complexity. The profile method is the most straightforward for full blocking.