1man
1man

Reputation: 5654

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.

Upvotes: 65

Views: 75336

Answers (5)

abbas dehghanzadeh
abbas dehghanzadeh

Reputation: 53

For new versions of selenium (4.24.0 is mine), the correct syntax will be as below:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

firefox_profile = webdriver.FirefoxProfile()
firefox_profile.set_preference("permissions.default.image", 2)
firefox_profile.update_preferences()

options = Options()
options.profile = firefox_profile
driver = webdriver.Firefox(options=options)

Upvotes: 2

user3453444
user3453444

Reputation: 59

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

Upvotes: 5

jacktrader
jacktrader

Reputation: 689

I personally could not get the "profile.default_content_setting_values" flag to work, so what worked for me was this:

options = Options()
options.add_extension('Block-image.crx')
driver = webdriver.Chrome(service=Service("chromedriver.exe"), options=options)

To get the "Block-Image.crx" file:

Browser to "http://crxextractor.com/" (or other crx download tool)

And provide the URL: https://chrome.google.com/webstore/detail/block-image/pehaalcefcjfccdpbckoablngfkfgfgj

The extension will download.

Rename crx file to "Block-Image.crx"

Upvotes: 0

rocky qi
rocky qi

Reputation: 1519

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

Upvotes: 136

Eduard Florinescu
Eduard Florinescu

Reputation: 17551

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

Upvotes: 3

Related Questions