0 votes
1 view
in Devops and Agile by (19.8k points)

I spend a lot of time searching for 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.

1 Answer

0 votes
by (63.4k points)

Disabling Images in Internet Explorer

Internet Explorer doesn’t provide an option for disabling images through command line, so we would need to use Registry for the same

from _winreg import *

 

key = OpenKey(HKEY_CURRENT_USER, r"Software\Microsoft\Internet Explorer\Main", 0, KEY_ALL_ACCESS)

SetValueEx(key, "Display Inline Images", 0, REG_SZ, "no")

CloseKey(key)

We just launch the browser and test it, once the images are disabled in registry, we just launch the browser and test it

from selenium import webdriver

driver = webdriver.Ie()

driver.get("https://www.intellipaat.com")

Disabling Images in Firefox

We need to change the profile settings, for disabling images in Firefox. Which can be done by using the code below

from selenium import webdriver

 

profile = webdriver.FirefoxProfile()

# 1 - Allow all images

# 2 - Block all images

# 3 - Block 3rd party images 

profile.set_preference("permissions.default.image", 2)

 

driver = webdriver.Firefox(firefox_profile=profile)

Disabling Images in Chrome

Long ago chrome used to support a command line flag --disable-images, which was deprecated. Now we need to use the chrome.prefs of the chromedriver. For more details refer this

from selenium import webdriver

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}

 

driver = webdriver.Chrome(chrome_options=option)

driver.get("https://www.intellipaat.com")

Disabling Images in PhantomJS

PhantomJS is a headless webkit browser. It can run without an X display on a server and very useful when we want to test a website in fast mode. Disabling images on the same will make the testing more favourable.

from selenium import webdriver

driver = webdriver.PhantomJS(service_args=["--load-images=no"])

driver.get("https://www.intellipaat.com")

driver.save_screenshot("intellipaat.png")

Hope this helps!

If you wish to Learn Selenium visit this Selenium Webdriver Tutorial.

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...