Python: Disable images in Selenium Google ChromeDriver

1man picture 1man · Jan 21, 2015 · Viewed 38.8k times · Source

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.

Answer

rocky qi picture rocky qi · Jul 23, 2015

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