How to Disable CSS in Python Selenium Using ChromeDriver with ChromeOptions
Problem Statement
Disabling CSS in Chrome Selenium using ChromeDriver is necessary to enhance page loading speed. While it's straightforward to disable images and JavaScript, attempting to disable CSS by modifying the 'profile.default_content_setting_values' preferences doesn't seem to work.
Solution
To disable CSS and show the page without it, you can use the following code:
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, 'css': 2}} # Add 'css': 2 to the dictionary to disable CSS 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')
Explanation
This code modifies the 'profile.default_content_setting_values' preferences by adding 'css': 2 to the dictionary. This effectively disables CSS. The browser options are then configured to maximize the window, disable the info bar, and disable extensions. Finally, the desired URL is loaded into the browser.
The above is the detailed content of How to Disable CSS in Chrome Selenium Using ChromeDriver with ChromeOptions?. For more information, please follow other related articles on the PHP Chinese website!