PhantomJS: JavaScript not executing on some pages


When using the PhantomJS browser to open web pages, you are faced with interesting/annoying problems. One such issue I encountered was that a custom click handler was not being executed on a specific page – while generally working on other pages.


In the browser log, the following error was showing up: Sys.WebForms.PageRequestManager is undefined.

After some digging it turns out that versions 4 and lower of the .NET frameworks do not send JavaScript files to unrecognized browsers. See this discussion for more details:
Sys.WebForms.PageRequestManager is undefined error in IE11, working fine in IE10 and below


So what is the solution? Yes, let’s spoof the user agent.

When using pure PhantomJS, this can be achieved by setting the page.settings.userAgent property (see PhantomJS API).

When using PhantomJS via Selenium Webdriver, you can pass the property as a capability when creating the browser object. In WebDriverJS (node.js) this results in the following:

var browser = webdriver.Capabilities.phantomjs();
browser.set('', '');

var driver = new webdriver.Builder()

As indicated here: How to change selenium user agent in selenium-webdriver nodejs land?. If you are unsure what to use as a user agent string, you can simply use your current browser’s one by visiting or which also provides an extensive list of used user agent strings.