Quantcast
Channel: Active questions tagged selenium - Stack Overflow
Viewing all articles
Browse latest Browse all 98775

Unable to disable downloading images in headless mode for Selenium Chrome WebDriver using Python3 [duplicate]

$
0
0

This question already has an answer here:

Im trying to disable my driver from loading/downloading images on a website. It should increase speed and also decrease the amount of time it takes to load a site as images wouldn't be downloaded, I still want to be able to find elements and interact with them like sending keys and clicking. The answer located here How to disable CSS in Python selenium using ChromeOptionsDOES not answer my question as it disables everything. Im unable to interact with elements. This defeats the purpose of selenium as I could just use requests.

Here is all my imports for reference.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

Im able to disable images when running a regular driver using the following code.

option = Options()
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}
option.add_argument("--start-maximized")
option.add_argument('window-size=1680,941')
driver = webdriver.Chrome(options=option,executable_path='path')
driver.get('https://yahoo.com/')

No images are loaded/downloaded when I go to 'yahoo.com' as seen in this screenshot https://imgur.com/a/cj2ylco

When I use the same code but add the 'headless' line it loads images.

option = Options()
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}
option.add_argument("--start-maximized")
option.add_argument('window-size=1680,941')
option.headless = True
driver = webdriver.Chrome(options=option,executable_path='path')
driver.get('https://yahoo.com/')
driver.save_screenshot('screen_shot2.png')

The driver still loads the images as seen in this screenshot https://imgur.com/a/krBpVCx is there any fix out there for this? Thank you!


Viewing all articles
Browse latest Browse all 98775

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>