7

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:

enter image description here

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

enter image description here

  • What is going on?
  • How do I fix it?
Community
  • 1
  • 1
nazmus saif
  • 167
  • 1
  • 2
  • 12

2 Answers2

3

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);
sax
  • 3,708
  • 19
  • 22
  • I don't understand. May you enlighten your explanations? – Stéphane Bruckert Nov 27 '14 at 15:07
  • I updated my answer, not sure if is the right way or I have to add a comment – sax Nov 27 '14 at 19:23
  • You said the logo disappeared because it's part of the CSS which has been disabled. Then, why can we still see the whole page layout and formatting? – Stéphane Bruckert Nov 28 '14 at 09:49
  • the layout is produced by the inline javascript, try profile.set_preference("javascript.enabled", False); to disable the javascript engine (not the remote sources) and the layout disappear – sax Nov 28 '14 at 19:35
  • ok now the images disappeared, but the layout is still working with firefox_profile.set_preference("javascript.enabled", False) and firefox_profile.set_preference("permissions.default.script", 2)... – nazmus saif Nov 29 '14 at 05:42
  • the original layout ? not in my test. Which version of selenium/firefox/OS are you using? I tested on selenium==2.44.0, firefox 33.1, OSX. NOTE: I think that the Stéphane screenshot is produces with the developer toolbar. I don't think this can be reproduced with selenium as is not part of the standard firefox. – sax Nov 29 '14 at 08:26
  • Images aren't displayed anymore, thanks for that. However stylesheets are still loaded, which is a big mystery I must say! – Stéphane Bruckert Nov 30 '14 at 19:13
3

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>")
deepak
  • 3,074
  • 2
  • 20
  • 29
  • As I see you got the bounty, I think this code is working! I appreciate that. But I don't have experience in selenium much, thats I'm not finding any way to use this code. Would you enlighten me with how to use "the selenium script executor" or where to put the script? @deepak – nazmus saif Dec 08 '14 at 15:09
  • @nazmussaif: have updated the answer. hope that helps. – deepak Dec 08 '14 at 17:50