Python: Disable images in Selenium Google ChromeDriver

I spend a lot of time searching about this. At the end of the day I combined a number of answers and it works. I share my answer and I'll appreciate it if anyone edits it or provides us with an easier way to do this.

1- The answer in Disable images in Selenium Google ChromeDriver works in Java. So we should do the same thing in Python:

opt = webdriver.ChromeOptions()
opt.add_extension("Block-image_v1.1.crx")
browser = webdriver.Chrome(chrome_options=opt)

2- But downloading "Block-image_v1.1.crx" is a little bit tricky, because there is no direct way to do that. For this purpose, instead of going to: https://chrome.google.com/webstore/detail/block-image/pehaalcefcjfccdpbckoablngfkfgfgj

you can go to http://chrome-extension-downloader.com/ and paste the extension url there to be able to download the extension file.

3- Then you will be able to use the above mentioned code with the path to the extension file that you have downloaded.


Here is another way to disable images:

from selenium import webdriver

chrome_options = webdriver.ChromeOptions()
prefs = {"profile.managed_default_content_settings.images": 2}
chrome_options.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(chrome_options=chrome_options)

I found it below:

http://nullege.com/codes/show/src@o@s@[email protected]/56/selenium.webdriver.ChromeOptions.add_experimental_option


Java: With this Chrome nor Firefox would load images. The syntax is different but the strings on the parameters are the same.

    chromeOptions = new ChromeOptions();
    HashMap<String, Object> images = new HashMap<String, Object>();
    images.put("images", 2);
    HashMap<String, Object> prefs = new HashMap<String, Object>();
    prefs.put("profile.default_content_setting_values", images);
    chromeOptions.setExperimentalOption("prefs", prefs);
    driver=new ChromeDriver(chromeOptions);

    firefoxOpt = new FirefoxOptions();
    FirefoxProfile profile = new FirefoxProfile();
    profile.setPreference("permissions.default.image", 2);
    firefoxOpt.setProfile(profile);

There is another way that comes probably to mind to everyone to access chrome://settings and then go through the settings with selenium I started this way just for didactic curiosity, but then I hit a forest of shadow-roots elements now when you encounter more than 3 shadow root element combined with dynamic content is clearly a way to obfuscate and make it impossible to automate, although might sound at least theoretically possible this approach looks more like a dead end, I will leave this answer with the example code, just for purely learning purposes to advert the people tempted to go to the challenge.. Not only was hard to find just the content settings due to the shadowroots and dynamic change when you find the button is not clickable at this point.

driver = webdriver.Chrome()


def expand_shadow_element(element):
  shadow_root = driver.execute_script('return arguments[0].shadowRoot', element)
  return shadow_root

driver.get("chrome://settings")
root1 = driver.find_element_by_tag_name('settings-ui')
shadow_root1 = expand_shadow_element(root1)

root2 = shadow_root1.find_element_by_css_selector('[page-name="Settings"]')
shadow_root2 = expand_shadow_element(root2)

root3 = shadow_root2.find_element_by_id('search')
shadow_root3 = expand_shadow_element(root3)

search_button = shadow_root3.find_element_by_id("searchTerm")
search_button.click()

text_area = shadow_root3.find_element_by_id('searchInput')
text_area.send_keys("content settings")

root0 = shadow_root1.find_element_by_id('main')
shadow_root0_s = expand_shadow_element(root0)


root1_p = shadow_root0_s.find_element_by_css_selector('settings-basic-page')
shadow_root1_p = expand_shadow_element(root1_p)


root1_s = shadow_root1_p.find_element_by_css_selector('settings-privacy-page')
shadow_root1_s = expand_shadow_element(root1_s)

content_settings_div = shadow_root1_s.find_element_by_css_selector('#site-settings-subpage-trigger')
content_settings = content_settings_div.find_element_by_css_selector("button")
content_settings.click()