【问题标题】:How to disable CSS in Python selenium using ChromeOptions如何使用 ChromeOptions 在 Python selenium 中禁用 CSS
【发布时间】:2018-08-08 10:38:11
【问题描述】:

我尝试在没有 CSS 的情况下显示页面以加快加载速度,并且我已设法使用以下代码禁用图像和 javascript:

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,但它不起作用。

我已经看到很多关于 Firefox 的答案,现在我想在 Chrome 中执行此操作。

【问题讨论】:

  • 您是否尝试显示没有 CSS 的页面或阻止下载 CSS 文件?
  • 我想显示没有 CSS 的页面以加快加载速度

标签: python css selenium google-chrome selenium-chromedriver


【解决方案1】:

要在没有 CSS 的情况下显示页面,以加快页面加载速度,您可以禁用 Preferences 用于存储单个内容设置的默认值,使用以下命令解决方案:

  • 代码块:

    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')
    
  • 浏览器快照:

【讨论】:

  • 我知道这是旧的,但我也在寻找答案 - 屏幕截图显示图像被禁用,但 CSS 不是,否则布局将无法正常工作,对吗?并且显示的代码块没有提到样式表......这是否意味着无法禁用 CSS?
【解决方案2】:

不幸的是,在 Chrome 中这是不可能的。

ChromeDriver disable rendering and CSS

Chrome/chromedriver 不能在没有像 Xserver 这样的虚拟屏幕的情况下无头运行,并且它不能不渲染 html 和 css。

允许的所有选项的详尽列表:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-10
    • 1970-01-01
    • 2013-04-30
    • 2021-09-06
    • 2021-01-31
    相关资源
    最近更新 更多