How to Disable CSS in Selenium Using ChromeOptions?

Linda Hamilton
Release: 2024-11-06 15:24:02
Original
574 people have browsed it

How to Disable CSS in Selenium Using ChromeOptions?

How to Disable CSS in Selenium Using ChromeOptions

When attempting to improve page loading speed by disabling CSS, users may encounter difficulties. While images and JavaScript can be disabled using specific options, simply changing the preference to disable CSS may not work.

Solution:

To disable CSS in Chrome using Selenium, a comprehensive approach is required, involving the modification of multiple preferences:

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}}
Copy after login

Additional ChromeOptions:

For a more robust experience, consider adding these options:

  • add_argument("start-maximized") - Maximize the browser window.
  • add_argument("disable-infobars") - Hide Chrome's info bars.
  • add_argument("--disable-extensions") - Disable browser extensions.

Example Usage:

from selenium import webdriver

options = webdriver.ChromeOptions()
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')
Copy after login

This will disable all preferences and start Chrome without CSS, providing improved loading times.

The above is the detailed content of How to Disable CSS in Selenium Using ChromeOptions?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!