16

I try to show the page without CSS to make loading faster, and I have managed to disable images and javascript using the following code:

option = webdriver.ChromeOptions()
prefs = {'profile.default_content_setting_values': {'images': 2, 'javascript': 2}}
option.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(chrome_options = option)

So, I changed:

{'profile.default_content_setting_values': {'images': 2, 'javascript': 2}}

to

{'profile.default_content_setting_values': {'css': 2}

and thought it would disable CSS, but it didn't work.

I have seen many answers for Firefox, now I want to do this in Chrome.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Cei1ing
  • 161
  • 1
  • 1
  • 4

3 Answers3

7

To show the page without CSS, to make page loading faster you can disable the Preferences used for storing the default values for the individual content settings using the following solution:

  • Code Block:

    from selenium import webdriver
    
    options = webdriver.ChromeOptions()
    prefs = {'profile.default_content_setting_values': {'cookies': 2, 'images': 2, 'javascript': 2, 
                                'plugins': 2, 'popups': 2, 'geolocation': 2, 
                                'notifications': 2, 'auto_select_certificate': 2, 'fullscreen': 2, 
                                'mouselock': 2, 'mixed_script': 2, 'media_stream': 2, 
                                'media_stream_mic': 2, 'media_stream_camera': 2, 'protocol_handlers': 2, 
                                'ppapi_broker': 2, 'automatic_downloads': 2, 'midi_sysex': 2, 
                                'push_messaging': 2, 'ssl_cert_decisions': 2, 'metro_switch_to_desktop': 2, 
                                'protected_media_identifier': 2, 'app_banner': 2, 'site_engagement': 2, 
                                'durable_storage': 2}}
    options.add_experimental_option('prefs', prefs)
    options.add_argument("start-maximized")
    options.add_argument("disable-infobars")
    options.add_argument("--disable-extensions")
    driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get('https://play.google.com/store')
    
  • Browser Snapshot:

Disabling_Preferences_storing_default_values

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 6
    I know this is old but I am looking for the answer too - the screenshot shows images are disabled, but CSS is not, otherwise the layout would not be working, right? And the code block shown does not mention stylesheets ... does it mean it is not possible to disable CSS? – Don't Panic Jun 11 '19 at 15:38
  • How would we do this with Edge driver? –  Jun 27 '23 at 23:55
3

Unfortunately, it is not possible in Chrome.

ChromeDriver disable rendering and CSS

Chrome/chromedriver cannot run headless without some virtual screen like Xserver, and it can't not-render the html and css.

Exhaustive list of all options that are allowed:

profile.default_content_setting_values:

cookies,
images,
javascript,
plugins,
popups,
geolocation,
notifications,
auto_select_certificate,
fullscreen,
mouselock,
mixed_script,
media_stream,
media_stream_mic,
media_stream_camera,
protocol_handlers,
ppapi_broker,
automatic_downloads,
midi_sysex,
push_messaging,
ssl_cert_decisions,
metro_switch_to_desktop,
protected_media_identifier,
app_banner,
site_engagement,
durable_storage
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Anchal Agrawal
  • 364
  • 2
  • 7
0

I've been wondering the same thing, and came across this (credit to https://www.scrapehero.com/how-to-increase-web-scraping-speed-using-puppeteer/)

It's not selenium, but in Pyppeteer you can intercept and modify network requests made by the page using setRequestInterception. This would allow you to intercept and abort css network requests, which would avoid downloading css files and their subsequent rendering.

Relevant Pyppeteer Docs

stephentgrammer
  • 470
  • 6
  • 16