Disable styling on Google Search with Selenium FirefoxDriver

怎甘沉沦 提交于 2019-12-09 05:56:27

问题


The following code disables stylesheets and images on a page loaded with Selenium Firefox webdriver:

from selenium import webdriver

firefox_profile = webdriver.FirefoxProfile()
firefox_profile.set_preference('permissions.default.stylesheet', 2)
firefox_profile.set_preference('permissions.default.image', 2)

driver = webdriver.Firefox(firefox_profile)
driver.get('http://www.stackoverflow.com/')

driver.close()

It works fine with stackoverflow.com, facebook.com, yahoo.com... but interestingly doesn't with Google Search; only the Google logo disappears and its stylesheet remains in place.

If you try with the following link http://google.com/search?q=nelson+mandela, you will get:

Whereas the expected result should look like this (no stylesheet + no picture):

  • What is going on?
  • How do I fix it?

回答1:


The google logo come form the css, where the pictures are embedded in the HTML as data (img src="data:image/jpeg;base64, ....) the code disable the loading of remote images not this type of sources

  • permissions.default.stylesheet: disable any formatting

  • permissions.default.image: disable any image and css background-image

if the image is embedded into the page as base64 encoded is not blocked by these permissions because is part of the HTML code (see http://en.wikipedia.org/wiki/Data_URI_scheme)

to disable more formatting you should add:

  • firefox_profile.set_preference("permissions.default.script", 2);
  • firefox_profile.set_preference("javascript.enabled", False);



回答2:


run the following javascript through the selenium script executor

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');
}

I have tested this with google, it works. got the above script from this firefox extension code

driver = webdriver.Firefox(firefox_profile)
driver.get('http://www.google.com/')
driver.execute_script("<put the above javascript here as string>")


来源:https://stackoverflow.com/questions/27108835/disable-styling-on-google-search-with-selenium-firefoxdriver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!