-
Notifications
You must be signed in to change notification settings - Fork 23
Troubleshooting
Before reporting an issue, make sure you are using the latest version:
phantomjs --version
All examples are designed to work with the latest version of PhantomJS. If some examples do not work, make sure that there is not more than one version of PhantomJS installed in the system. Multiple versions may lead to conflict as to which one is being invoked in the terminal.
If the data is not transferred correctly, check if the network works as expected.
Since every network request and response can be "sniffed", add simple callbacks to facilitate such troubleshooting. For more details, see the wiki page on Network Monitoring. An example of a simple logging:
page.onResourceRequested = function (request) {
console.log('Request ' + JSON.stringify(request, undefined, 4));
};
Transport Layer Security (TLS) and Secure Sockets Layer (SSL) are necessary to access encrypted data, for example when connecting to a server using HTTPS. Thus, if PhantomJS works well with HTTP but it shows some problem when using HTTPS, the first useful thing to check it whether the SSL libraries, usually OpenSSL, have been installed properly.
On Windows, the default proxy setting may cause a massive network latency (see issue 580). The workaround is to disable proxy completely, e.g. by launching PhantomJS with ----proxy-type=none
command-line argument.
To easily catch an error occured in a web page, whether it is a syntax error or other thrown exception, an onError
handler for the WebPage object has been added. An example on such a handler is:
page.onError = function (msg, trace) {
console.log(msg);
trace.forEach(function(item) {
console.log(' ', item.file, ':', item.line);
})
}
Now if the page opens a site with some JavaScript exceptions, a detailed information (including the stack trace) will be printed out.
Remote debugging permits inspection of the script and web page via another WebKit-based browser (Safari and Chrome). This is achieved by launching PhantomJS with the new option, as in this example
phantomjs --remote-debugger-port=9000 test.js
After than, open Safari/Chrome and go to the http://ipaddress:9000. The browser will show the familiar Web Inspector interface which in this case works on the script being tested.
To run your script, simply enter the __run()
command in the Web Inspector Console. Alternatively, use --remote-debugger-autorun=yes
command-line argument to have your script launched immediately.
To debug inside a target web page requires two inspectors and a multi-step process using a function like this:
function debugPage() {
console.log("Refresh a second debugger-port page and open a second webkit inspector for the target page.");
console.log("Letting this page continue will then trigger a break in the target page.");
debugger; // pause here in first web browser tab for steps 5 & 6
page.evaluateAsync(function() {
debugger; // step 7 will wait here in the second web browser tab
});
}
- start on command line with remote-debugger-port option.
- navigate to debugging port in web browser
- get first web inspector for phantom context
- from the web browser console execute
__run()
, which will hit first debugger point - navigate to debugging port in a second web browser tab
- get second web inspector (for page context)
- return to the first web inspector tab and click continue on debugger
- navigate back to second tab and you should find debugger waiting
If PhantomJS is launched without any argument, it starts in the so-called interactive mode, also known for REPL (read-eval-print-loop). This mode allows a faster cycle of experiment and script prototyping. PhantomJS REPL supports the expected features: command editing, persistent history, and autocomplete (with Tab key). Read more about REPL.