Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Scan for outdated or missing drivers - takes under a minute3Repair Windows errors before they cause bigger problemsSome links on this page are affiliate links: if you buy through them we may earn a commission, at no extra cost to you.
Use page.onError in PhantomJS to capture uncaught page exceptions and their stack frames; use page.onConsoleMessage for messages written through the page’s console API. In Selenium, browser logs and JavaScript errors are separate from Selenium client or driver errors. The legacy browser-log API works only where the browser and driver support it; Selenium’s newer event-based route is WebDriver BiDi, which must be enabled for the browser session. The right method depends on which signal you need and the versions of PhantomJS, GhostDriver, Selenium, and the browser in your test.
First identify which error you need to see
“JavaScript error” can mean several different things. Choosing the wrong channel is a common reason a log appears empty: a page’s uncaught exception is not necessarily a console message, and neither is the same as a failure in your Selenium test code or a message printed by the browser-driver process.
| Signal | Where it originates | Starting point |
|---|---|---|
| Uncaught page exception | Page JavaScript throws an exception, including a syntax error | PhantomJS page.onError; for Selenium, a browser’s supported logging events or BiDi JavaScript-error handler |
| Page console message | Page calls a console API such as console.log or console.error |
PhantomJS page.onConsoleMessage; for Selenium, a supported browser log or BiDi console-message handler |
| Selenium test failure | Your test code, WebDriver command, or assertion | Catch or report it in the test runner; this is not a browser page log |
| Browser or driver diagnostic | The browser or driver process | Inspect the process output or configured driver log separately |
When diagnosing an unknown failure, collect the page exception and console channels separately, then inspect the driver output if the browser session itself is failing.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Capture uncaught JavaScript exceptions in PhantomJS
PhantomJS documents page.onError as a handler for errors in a page, including syntax errors and other thrown exceptions. It receives an error message and trace information. The trace frames provide the source file and line number when available. The following standalone PhantomJS script prints both:
#1 Best Overall
var page = require('webpage').create();
page.onError = function (message, trace) {
console.error('Page JavaScript exception: ' + message);
if (trace && trace.length) {
trace.forEach(function (frame) {
console.error(' ' + frame.file + ':' + frame.line);
});
}
};
page.open('https://example.com', function (status) {
if (status !== 'success') {
console.error('Could not load the page: ' + status);
phantom.exit(1);
return;
}
// The page's onError callback is registered before navigation.
// Keep the process alive long enough for page scripts to run.
window.setTimeout(function () {
phantom.exit();
}, 1000);
});
Save it as capture-errors.js and run it with the PhantomJS executable installed in your environment, for example phantomjs capture-errors.js. Register the handler before opening the URL so it is in place as page scripts execute. The delay is a simple example, not a guarantee that every page has finished asynchronous work; choose a wait appropriate to the page under test.
PhantomJS’s troubleshooting documentation shows printing each trace frame’s file and line. A missing trace does not mean there was no error: check the message, and account for errors whose source details are unavailable. See the PhantomJS troubleshooting guide.
Capture page console messages in PhantomJS
Use page.onConsoleMessage for messages emitted through the page’s JavaScript console API. PhantomJS does not display page console messages by default, so redirecting them in this handler is a practical way to see them in the script’s output:
Rank #2
- Programming Code Console Log Javascript Debugging T-shirt. Funny Console Log design perfect for computer geeks, frontend developers, programmers, IT specialist, or engineers. Perfect for men women or anyone who love code and programming as a gift birthda.
- Great gift idea for anybody who works with or as an IT professionals, computer scientists, developers, programmers, software engineers, coders, and anyone with an interest in Javascript, HTML, and any other languages. Wear it to the office or anywhere!
- Hardcover journal with 240 line-ruled pages (120 sheets)
- Built-in elastic closure and ribbon bookmark
- Includes an expandable inner storage pocket and a pen holder
var page = require('webpage').create();
page.onConsoleMessage = function (message, lineNumber, sourceId) {
console.log('[page console] ' + message);
if (lineNumber !== undefined && sourceId) {
console.log(' at ' + sourceId + ':' + lineNumber);
}
};
page.onError = function (message, trace) {
console.error('[uncaught page exception] ' + message);
(trace || []).forEach(function (frame) {
console.error(' at ' + frame.file + ':' + frame.line);
});
};
page.open('https://example.com', function (status) {
if (status !== 'success') {
console.error('Could not load the page: ' + status);
phantom.exit(1);
return;
}
window.setTimeout(function () { phantom.exit(); }, 1000);
});
The callback’s documented arguments can include the message, line number, and source identifier. The API reference cautions that line number and source identifier were not used in PhantomJS versions at or below 1.8.1, so do not rely on those fields for old builds. PhantomJS onConsoleMessage API.
Do not treat console.error and an uncaught exception as equivalent. An old issue report describes different console.error behavior across PhantomJS packages or builds; it is anecdotal and version-specific, not a compatibility guarantee. If the distinction matters, register both handlers and verify their output with the exact PhantomJS build you deploy. PhantomJS issue report.
When Selenium is driving PhantomJS through GhostDriver
GhostDriver documents optional loggingPrefs for browser and har logs, with both off by default. If your Selenium test uses the PhantomJS driver, check the logging preferences supported by that particular Selenium binding and GhostDriver combination. Enabling a preference is necessary if you expect that remote log to be collected, but it does not establish that every build exposes every browser message in the same way.
Rank #3
- Confirm that you are requesting the browser log rather than looking only at Selenium’s own exception output.
- Check the session capabilities or preferences used to create the PhantomJS session and whether browser logging is enabled.
- Capture the PhantomJS/GhostDriver process output separately; driver diagnostics are not page browser logs.
- Test the exact deployed Selenium binding and GhostDriver version with a page that emits both a console message and an uncaught exception.
The GhostDriver README documents the logging preferences and their default state, but does not provide a universal compatibility matrix for every Selenium binding and version. GhostDriver README.
The Tool Desk
Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Read Selenium’s legacy browser log API when supported
Selenium’s JavaScript API documents a pull-style browser log call: request the browser log after the page action, then iterate over its entries. For the JavaScript binding, the documented API shape is:
const { logging } = require('selenium-webdriver');
const entries = await driver.manage().logs().get(logging.Type.BROWSER);
for (const entry of entries) {
console.log(`${entry.level.name}: ${entry.message}`);
}
Call this while the WebDriver session is still active and after the interaction that may have generated messages. The API returns entries already recorded; it does not install a streaming callback. Handle errors around retrieval in the test runner because an unsupported browser/driver may reject or fail to provide this log type.
Rank #4
- Programming Code Console Log Javascript Debugging T-shirt. Funny Console Log design perfect for computer geeks, frontend developers, programmers, IT specialist, or engineers. Perfect for men women or anyone who love code and programming as a gift birthda.
- Great gift idea for anybody who works with or as an IT professionals, computer scientists, developers, programmers, software engineers, coders, and anyone with an interest in Javascript, HTML, and any other languages. Wear it to the office or anywhere!
- Hardcover journal with 240 line-ruled pages (120 sheets)
- Built-in elastic closure and ribbon bookmark
- Includes an expandable inner storage pocket and a pen holder
This API is conditional, not universal. Selenium’s JavaScript logging documentation labels remote logging a non-standard feature supported by only a few browsers; it notes basic Firefox support and Chrome performance logging options. Those notes do not mean every browser session provides the same browser-console entries. Confirm support for your browser, driver, and Selenium release before making the log a required test artifact. Selenium JavaScript logging API.
Use WebDriver BiDi for event-based Selenium logging
Selenium documents WebDriver BiDi as a WebSocket-based bidirectional protocol that can stream browser events, including console messages and JavaScript errors. Unlike the legacy retrieval call, event handlers let a test react when an event arrives rather than asking for accumulated entries afterward.
- Use a browser and driver combination that implements the BiDi events your test needs.
- Enable BiDi in the browser options when creating the WebDriver session. The exact option syntax varies by Selenium language binding.
- Register the logging handlers for console messages and JavaScript errors using the API documented for that binding.
- Keep the session and handlers alive while the page action runs, then remove handlers or close the session as appropriate for your test.
Use Selenium’s BiDi logging examples for binding-specific setup and handler syntax rather than transplanting option names from another language. The WebDriver BiDi overview describes BiDi as the standards-based direction for bidirectional events and a cross-browser alternative to CDP. “Cross-browser” does not mean every browser, driver, or release implements every event identically; validate the exact versions pinned in your project.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Choose a method by the signal and the automation stack
| Stack | Exception signal | Console signal | Collection style and caveat |
|---|---|---|---|
| Direct PhantomJS WebPage | page.onError |
page.onConsoleMessage |
Callbacks; confirm fields and behavior on the deployed legacy build |
| Selenium with PhantomJS/GhostDriver | Check browser-log behavior and capture process output separately | Check browser logging preferences | GhostDriver documents browser/HAR preferences as off by default; binding/version support varies |
| Selenium legacy browser logs | Available only if the browser/driver exposes relevant entries | Browser log entries, if supported | Pull-style, non-standard remote logging; not universal |
| Selenium WebDriver BiDi | Register the JavaScript-error event handler | Register the console-message handler | Event-based; enable BiDi in options and verify event support for the pinned browser/driver |
Troubleshoot empty or incomplete logs
No output from PhantomJS
- Handler registered too late: assign the callback before
page.open; page scripts may run as the document loads. - Wrong channel: use
onErrorfor uncaught exceptions andonConsoleMessagefor console API messages. Register both when unsure. - Process exits early: do not exit immediately after navigation if scripts or asynchronous work still need to run; wait for a relevant page condition or a suitable delay.
- Old build: line/source metadata is not used in PhantomJS versions at or below 1.8.1; print the message even when those fields are absent.
Selenium returns an empty browser log
- Logging disabled: with GhostDriver, verify its browser logging preference because the documented default is off.
- Unsupported remote logging: Selenium’s legacy log API is not implemented by every browser/driver. Verify support rather than assuming an empty list proves the page had no messages.
- Wrong browser context: distinguish page logs from driver-process output and test-runner exceptions.
- Read at the wrong time: retrieve legacy entries after the action expected to generate them and before ending the session.
BiDi handler never fires
- Check that BiDi was enabled in the options used to create this session, not added after session creation.
- Confirm the binding-specific handler API and event type in Selenium’s examples.
- Check support for the specific browser, driver, and Selenium versions. The published guidance does not establish a complete compatibility matrix.
- Run a small page that deliberately calls a console API and throws an exception, and verify each signal independently.
Performance, reliability, and cost considerations
Logging adds output and can produce a large volume when a page or test suite emits many messages. Capture only the channels you need for routine runs, and reserve broader diagnostics for investigation. For reproducibility, record the Selenium binding, browser, driver, and PhantomJS/GhostDriver versions with the test results; behavior should be checked against the versions actually deployed rather than inferred from an API name.
None of the cited API documentation establishes a universal cost, throughput, or performance figure for these logging methods. A remote browser-log call adds a retrieval step; BiDi uses a live event connection; PhantomJS handlers execute within the page automation process. Choose based on available support and whether you need callbacks or an accumulated log, then measure within your own test environment.
Or skip the browser setup
If what you need is a rendered page screenshot rather than JavaScript log entries, ScreenshotNeo offers a single-request screenshot API. It does not replace the error-capture methods above or return browser console logs. Its clean-shot workflow accepts cookie/consent banners and removes known consent platforms, newsletter popups, and chat widgets before capture; bot checks, blank pages, and failed loads are not billed. It also provides an MCP server for AI agents, and the free plan includes 1,000 screenshots a month with no card; paid plans start at $5 for 3,000 shots.
Recommended Free Tools
Example cURL request, with API details in the ScreenshotNeo documentation:
curl -G "https://api.screenshotneo.com/v1/shot"
-d access_key=YOUR_API_KEY
--data-urlencode url=https://example.com
-o shot.webp
Sign up for 1,000 free screenshots a month with no card.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

