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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.