如何使用ChromeOptions在Python硒中禁用CSS [英] How to disable CSS in Python selenium using ChromeOptions
问题描述
我尝试显示不带CSS的页面以加快加载速度,并且我设法使用以下代码禁用了图片和JavaScript:
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)
因此,我将{'profile.default_content_setting_values': {'images': 2, 'javascript': 2}}
更改为{'profile.default_content_setting_values': {'css': 2}
,并认为它将禁用CSS,但是它不起作用.
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.
我已经看到了许多有关Firefox的答案,现在我想在Chrome中做到这一点.
I have seen many answers for Firefox, now I want to do this in Chrome.
推荐答案
要显示没有 CSS 的页面,要使页面加载速度更快,您可以禁用 首选项 用于使用以下解决方案存储各个内容设置的默认值:
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:
这篇关于如何使用ChromeOptions在Python硒中禁用CSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!