Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python, Selenium: how to specify which sites can load images?

In Selenium chromdriver, we can disable images in this way:

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)

But this method disables images on all sites. In Google Chrome, you can separately specify a list of sites to which images can be loaded.

Is it possible to do this with Selenium?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

No, you won’t be able to configure ChromeOptions differently per site unless you reinitialize ChromeDriver and

When you configure an instance of a ChromeDriver using ChromeOptions() in the process of initiating a new Chrome Browsing Session the configuration gets baked into the ChromeDriver service instance and will persist till the lifetime of the WebDriver being uneditable. So you can’t add any further ChromeOptions to the WebDriver instance which is currently in execution.

Even if you are able to extract the ChromeDriver and ChromeSession attributes e.g. Session ID, Cookies, UserAgent and other session attributes from the already initiated ChromeDriver and Chrome Browsing Session still you won’t be able to change the set of attributes of the ChromeDriver.

A cleaner way would be to call driver.quit() within tearDown(){} method to close and destroy the current ChromeDriver and Chrome Browser instances gracefully and then span a new set of ChromeDriver and Chrome Browser instance with the new set of configurations.


tl; dr

You can find a couple of relevant discussions in:

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading