Disable Images in Selenium Python

Disable images in Selenium Python with chromedriver

Preference are not supported in headless mode so a universal method would be to add arguments :

from selenium import webdriver

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--blink-settings=imagesEnabled=false')
driver = webdriver.Chrome(options=chrome_options)

driver.get("https://www.google.com/search?newwindow=1&safe=off&hl=en&gl=ar&tbm=isch&sxsrf=ALeKk02lEcMPPT8VE72p7l8mkzkQmdAtqA%3A1615809915268&source=hp&biw=1920&bih=937&ei=e01PYPuIDszDgQa76aHIBQ&q=stackoverflow+meme&oq=stackov&gs_lcp=CgNpbWcQAxgAMgIIADICCAAyAggAMgIIADICCAAyAggAMgIIADICCAAyAggAMgIIADoECCMQJzoICAAQsQMQgwE6BQgAELEDULcRWMYaYLcjaABwAHgAgAFGiAHoApIBATeYAQCgAQGqAQtnd3Mtd2l6LWltZw&sclient=img")

driver.get("https://www.google.com/search?q=stackoverflow+&tbm=isch&ved=2ahUKEwjL7MOCobLvAhVUweYKHYNYC3oQ2-cCegQIABAA&oq=stackoverflow+&gs_lcp=CgNpbWcQAzIECCMQJzIECAAQQzIECAAQQzICCAAyAggAMgIIADIECAAQQzICCAAyAggAMgIIAFDtvQFY7b0BYIq_AWgAcAB4AIABPogBPpIBATGYAQCgAQGqAQtnd3Mtd2l6LWltZ8ABAQ&sclient=img&ei=gE1PYMusCtSCmweDsa3QBw&bih=937&biw=1920&gl=ar&safe=off&hl=en")

This will disable all the images

Python Selenium Chromedriver Can't disable images to load

I managed to do it:

option = webdriver.ChromeOptions()
chrome_prefs = {}
option.experimental_options["prefs"] = chrome_prefs
chrome_prefs["profile.default_content_settings"] = {"images": 2}
chrome_prefs["profile.managed_default_content_settings"] = {"images": 2}
PATH = 'C:\Program Files (x86)\chromedriver.exe'
browser = webdriver.Chrome(executable_path = PATH, options=option)
browser.get('https://www.yahoo.com/')
browser.find_element_by_xpath('//*[@class="btn primary"]').click()

Disable image load in Chrome with Selenium in Python

option = webdriver.ChromeOptions()
chrome_prefs = {}
option.experimental_options["prefs"] = chrome_prefs
chrome_prefs["profile.default_content_settings"] = {"images": 2}
chrome_prefs["profile.managed_default_content_settings"] = {"images": 2}
browser = webdriver.Chrome(chrome_options=option)

Disable images in Selenium Python

UPDATE: The answer might not work any longer since permissions.default.image became a frozen setting and cannot be changed. Please try with quickjava extension (link to the answer).


You need to pass firefox_profile instance to the webdriver constructor:

from selenium import webdriver

firefox_profile = webdriver.FirefoxProfile()
firefox_profile.set_preference('permissions.default.stylesheet', 2)
firefox_profile.set_preference('permissions.default.image', 2)
firefox_profile.set_preference('dom.ipc.plugins.enabled.libflashplayer.so', 'false')

driver = webdriver.Firefox(firefox_profile=firefox_profile)
driver.get('http://www.stackoverflow.com/')

driver.close()

And this is how it would be displayed:

Sample Image

Disable images in Selenium Google ChromeDriver

Use http://chrome-extension-downloader.com/ to download the "Block Image" extension (https://chrome.google.com/webstore/detail/block-image/pehaalcefcjfccdpbckoablngfkfgfgj?hl=en-GB). The extension prevents the image from being downloaded in the first place. Now it's just a matter of loading it using the following statement:

    var options = new ChromeOptions();

//use the block image extension to prevent images from downloading.
options.AddExtension("Block-image_v1.0.crx");

var driver = new ChromeDriver(options);


Related Topics



Leave a reply



Submit