[英]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. 但是,这只会删除一些图像,而不会删除所有内容,如下所示。
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搜索上禁用样式,结果仍然不尽相同。
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: 浏览器快照:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.