How to block image loading in Selenium?

When web scraping using Selenium we are wasting a lot of connection bandwidth for loading images. Unless we're capturing screenshots our data scrapers don't need to actually see the various visuals like images.

To block images in Selenium we have two options either add imagesEnabled=false flag or set profile.managed_default_content_settings.images value to 2:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.headless = True

chrome_options = webdriver.ChromeOptions()
# this will disable image loading
chrome_options.add_argument('--blink-settings=imagesEnabled=false')
# or alternatively we can set direct preference:
chrome_options.add_experimental_option(
    "prefs", {"profile.managed_default_content_settings.images": 2}
)

driver = webdriver.Chrome(options=options, chrome_options=chrome_options)
driver.get("https://www.twitch.tv/directory/game/Art")
driver.quit()
Question tagged: Selenium, Python

Related Posts

How to Scrape With Headless Firefox

Discover how to use headless Firefox with Selenium, Playwright, and Puppeteer for web scraping, including practical examples for each library.

Selenium Wire Tutorial: Intercept Background Requests

In this guide, we'll explore web scraping with Selenium Wire. We'll define what it is, how to install it, and how to use it to inspect and manipulate background requests.

Web Scraping Dynamic Web Pages With Scrapy Selenium

Learn how to scrape dynamic web pages with Scrapy Selenium. You will also learn how to use Scrapy Selenium for common scraping use cases, such as waiting for elements, clicking buttons and scrolling.