0

How to allow just one image to load with selenium chrome python?

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)

The above code works for blocking all images, but i want to load just one and block the others.

petezurich
  • 9,280
  • 9
  • 43
  • 57
nitorv
  • 1

1 Answers1

0

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


Details

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:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352