简体   繁体   English

像开发人员工具扩展一样,如何从Selenium中的Firefox中删除所有CSS样式

[英]How to remove all CSS styling from Firefox in Selenium like the Developer Tools extension does

I'm aware this question has been asked before, the issue is that none of the solutions do the job quite the same as the Developer tools extension which is what I'm in need of. 我知道之前曾有人问过这个问题,问题是没有一个解决方案能像我需要的开发人员工具扩展一样完成工作。 This is achieved by going into the WebDeveloper extension menu and clicking 'Disable all styling' in the css section. 这是通过进入WebDeveloper扩展菜单并单击css部分中的“禁用所有样式”来实现的。

I need the page to look like this for some automation. 我需要页面看起来像这样一些自动化。

Needed Format (No CSS styling at all) 所需格式(完全没有CSS样式)

The first solution is 第一个解决方案是

from selenium.webdriver.firefox.firefox_profile import FirefoxProfile

def disableImages(self):
## get the Firefox profile object
firefoxProfile = FirefoxProfile()
## Disable CSS
firefoxProfile.set_preference('permissions.default.stylesheet', 2)
## Disable images
firefoxProfile.set_preference('permissions.default.image', 2)
## Disable Flash
firefoxProfile.set_preference('dom.ipc.plugins.enabled.libflashplayer.so',
                              'false')
## Set the modified profile while creating the browser object 
self.browserHandle = webdriver.Firefox(firefoxProfile)

However, this only removes some images and not everything as you can see below. 但是,这只会删除一些图像,而不会删除所有内容,如下所示。

1st Solution 第一种解决方案

Then I found a script 然后我找到了一个脚本

var queries = ['link[rel=stylesheet][href]', 'style'];
for (var i = 0; i < queries.length; i++) {
    var remove = document.querySelectorAll(queries[i]);
    for (var j = 0; j < remove.length; j++) {
        remove[j].outerHTML = '';
    }
}
var inline = document.querySelectorAll('*[style]');
for (var i = 0; i < inline.length; i++) {
    inline[i].removeAttribute('style');
}

The full explanation of how it worked is on this answer Disable styling on Google Search with Selenium FirefoxDriver which still did not turn out the same. 关于它如何工作的完整解释是在此答案上,使用Selenium FirefoxDriver在Google搜索上禁用样式,结果仍然不尽相同。

2nd Solution 第二解决方案

Any help would be greatly appreciated? 任何帮助将不胜感激? Thanks in advance. 提前致谢。

Seems you were pretty close. 看来您已经很接近了。

I have added two additional preferences in addition to the set you have used and I am able to achieve the desired results ie get rid of all the CSS Styling within Firefox . 除了您使用的设置外,我还添加了两个其他首选项 ,并且能够实现所需的结果,即摆脱了Firefox中的所有CSS样式

  • Code Block: 代码块:

     from selenium import webdriver firefoxProfile = webdriver.FirefoxProfile() firefoxProfile.set_preference('permissions.default.stylesheet', 2) firefoxProfile.set_preference('permissions.default.image', 2) firefoxProfile.set_preference('dom.ipc.plugins.enabled.libflashplayer.so', 'false') firefoxProfile.set_preference("permissions.default.script", 2); firefoxProfile.set_preference("javascript.enabled", False); browserHandle = webdriver.Firefox(firefox_profile=firefoxProfile, executable_path=r'C:\\Utility\\BrowserDrivers\\geckodriver.exe') browserHandle.get('https://play.google.com/store') 
  • Browser Snapshot: 浏览器快照:

play_google_com_store

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 python selenium 访问 google chrome 开发人员工具上的安全面板? - How to access Security panel on google chrome developer tools with python selenium? Selenium-如何从现有的Firefox配置文件导入所有设置 - Selenium - How to import all settings from an existing Firefox profile 如何从 Selenium 脚本中获取 Firefox Web 扩展的“内部 UUID”? - How to get Firefox Web Extension's "Internal UUID" from Selenium script? 从 Firefox 到 Selenium 的所有 Cookies - All Cookies from Firefox over Selenium 如何清理所有 Selenium Firefox 进程 - How to clean up all Selenium Firefox Processes 如何使用 Selenium 从 Firefox 删除消息浏览器处于远程控制之下 - How to remove the message Browser is under remote control from Firefox using Selenium 如何使用浏览器开发人员工具从 POST 方法获取数据 - How to get data from POST method using browser developer tools 如何从 Chrome 中的“开发人员工具”中抓取完整的 html 代码? - How to scrape the full html code from "developer tools" in Chrome? 如何使用 python selenium webdriver 在 Chrome 开发人员工具控制台中进行 fetch 调用 - How to make a fetch call in Chrome developer tools console using python selenium webdriver 硒无法连接到Firefox - selenium does not connect to firefox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM