python - 如何使用 ChromeOptions 在 Python selenium 中禁用 CSS

标签 python css selenium google-chrome selenium-chromedriver

我尝试在没有 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 的页面,为了加快页面加载速度,您可以禁用 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')
    
  • 浏览器快照:

Disabling_Preferences_storing_default_values

关于python - 如何使用 ChromeOptions 在 Python selenium 中禁用 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64024606/

相关文章:

python - 使用 numpy 将棕褐色效果应用于 3D 数组

python - 为什么列为键字典,仍然会显示为元组作为键字典

python - Pyside2 - qml 返回一个空窗口

python - 是否有必要传达(或预期)Python Socket TCP Buff 大小

ruby-on-rails - 帮助将两种不同的样式应用于 Ruby on Rails 复数化方法

javascript - 从高度 : 0px to auto failing on first click 开始动画 div

javascript - Nightwatch 集成测试 : Can't select option from select dropdown in Safari

css - 格式化 CSS 时防止缩进

java - Selenium 3.0 Firefx 驱动程序失败,出现 org.openqa.selenium.SessionNotCreatedException : Unable to create new remote session

http - selenium 网格和节点 URL 可以安全使用吗?