1

There seems to be confusion over whether it's possible to disable CSS when using the PhantomJS webdriver for Selenium. It appears to definitely be possible when using FireFox by adapting the FireFox profile, but I'm hoping to use it for PhantomJS since it is generally faster than FireFox.

Is it possible to disable CSS in this instance? If so, could you provide some idea of how to implement it?

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
chris
  • 1,869
  • 4
  • 29
  • 52

1 Answers1

1

PhantomJS doesn't seem to have an option to disable CSS. You can work around this limitation by removing the CSS yourself:

driver.execute_script("""
    var toRemove = [];
    toRemove.push.apply(toRemove, document.querySelectorAll('link[type*=\"/css\"]'));
    toRemove.push.apply(toRemove, document.querySelectorAll('style'));
    toRemove.forEach(function(s){
        s.parentNode.removeChild(s);
    });
    [].forEach.call(document.querySelectorAll('[style]'), function(e){
        e.removeAttribute('style');
    });
""")

This removes all linked, local and inline styles and leaves the default browser style alone. You might want to add some kind of reset stylesheet.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
  • it looks like this just removes css that has been downloaded, but is it possible to not download the css in the first place? thanks for your help! – chris Feb 27 '16 at 14:31
  • Here you go: [Prevent CSS/other resource download in PhantomJS/Selenium driven by Python](http://stackoverflow.com/q/19099070/1816580) – Artjom B. Feb 27 '16 at 15:29