Search code examples
javascriptseleniumphantomjsghostdriver

Why is not possible to disable JavaScript in GhostDriver / PhantomJSDriver?


I would like to disable JavaScript in PhantomJSDriver on a specific page, but this link here says that it not possible, because "The whole GhostDriver will become unusable".

Can someone please explain me, as if I am 5 years old, why this is so?

Do I have any options to somehow prevent PhantomJSDriver from executing JavaScript on a page?


Solution

  • GhostDriver is written in JavaScript and it uses the PhantomJS API to translate WebDriver wire protocol commands into native PhantomJS commands/calls.

    PhantomJS has two contexts: the outer (phantom) context to drive the browser and the inner (page) context where the page JavaScript is executed. If you disable JavaScript in PhantomJS, only the page context is disabled. You can still do some things, but you cannot use page.evaluate*() anymore which is the door into the page context. If you look closely into the PhantomJS API, you see that there are no functions that can be used to access the DOM. This means that you cannot find an element, you cannot query for the text of an element, you cannot change an element and you cannot know where an element is to click on it.

    All you can do, is creating screenshots (page.render()) and blindly clicking and typing (page.sendEvent()). This is not enough for the WebDriver protocol to work.