You can block image loading in Selenium by passing in the custom ChromeOptions
object and setting the appropriate content settings preferences.
Here is some sample code that navigates to the ScrapingBee homepage while blocking images:
from selenium import webdriver
from selenium.webdriver.common.by import By
DRIVER_PATH = '/path/to/chromedriver'
# Block images via ChromeOptions object
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.managed_default_content_settings.images": 2}
chrome_options.add_experimental_option("prefs", prefs)
# Pass in custom options while creating a Chrome object
driver = webdriver.Chrome(options=chrome_options, executable_path=DRIVER_PATH)
# Navigate to ScrapingBee while blocking all images
driver.get("http://www.scrapingbee.com")
The code for Firefox looks fairly similar as well:
from selenium import webdriver
from selenium.webdriver.common.by import By
DRIVER_PATH = '/path/to/geckodriver'
# Block images via FirefoxOptions object
options = webdriver.FirefoxOptions()
options.set_preference("permissions.default.image", 2)
# Pass in custom options while creating a Firefox object
webdriver.Firefox(options=options, executable_path=DRIVER_PATH)
# Navigate to ScrapingBee while blocking all images
driver.get("http://www.scrapingbee.com")