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()

Provided by Scrapfly

This knowledgebase is provided by Scrapfly — a web scraping API that allows you to scrape any website without getting blocked and implements a dozens of other web scraping conveniences. Check us out 👇