ScreenshotNeo

BlogHow-to

How to Execute JavaScript After a Full Webpage Loads in PhantomJS

Run JavaScript after PhantomJS finishes loading a page, handle dynamic content, avoid early exits, and troubleshoot common callback and evaluate errors.

By the ScreenshotNeo team30 September 20267 min read

How to Execute JavaScript After a Full Webpage Loads in PhantomJS

Use the callback passed to page.open. PhantomJS invokes it when page loading finishes and supplies a status. Check for success, then call page.evaluate inside that callback to run JavaScript in the webpage context. Call phantom.exit() only after the final asynchronous work is complete.

var page = require('webpage').create();

page.open('https://example.com', function (status) {
  if (status !== 'success') {
    console.log('Unable to load the page.');
    phantom.exit(1);
    return;
  }

  var result = page.evaluate(function () {
    return document.title;
  });

  console.log(result);
  phantom.exit();
});

The page.open callback is the local form of PhantomJS’s onLoadFinished event. The event means that PhantomJS considers navigation finished; it does not prove that every timer, API request, or client-side render has completed. See the official page.open documentation, onLoadFinished documentation, and page.evaluate documentation.

1. Understand the PhantomJS loading lifecycle

  1. Create a WebPage object with require('webpage').create().
  2. Start navigation with page.open(url, callback).
  3. Read the callback’s status. PhantomJS reports success when there were no network errors and fail when a network error occurred.
  4. Run page code with page.evaluate. This function executes inside the loaded document, not in the outer PhantomJS script.
  5. Return simple serializable values such as strings, numbers, booleans, arrays, or plain objects.
  6. Exit only after the callback and any later asynchronous operation have finished.

For a single navigation, the page.open callback is usually the clearest hook. For reusable page setup, assign page.onLoadFinished before calling page.open:

The page.open callback runs after PhantomJS reports that navigation has finished.
The page.open callback runs after PhantomJS reports that navigation has finished.
var page = require('webpage').create();

page.onLoadFinished = function (status) {
  if (status !== 'success') {
    console.log('Load failed: ' + status);
    phantom.exit(1);
    return;
  }

  var data = page.evaluate(function () {
    return {
      title: document.title,
      links: document.querySelectorAll('a').length
    };
  });

  console.log(JSON.stringify(data));
  phantom.exit();
};

page.open('https://example.com');

2. Execute DOM JavaScript after loading

Use page.evaluate for DOM reads and changes. The function is sandboxed in the page context, so it cannot access the outer script’s page or phantom objects.

var page = require('webpage').create();

page.open('https://example.com', function (status) {
  if (status !== 'success') {
    console.error('Navigation status: ' + status);
    phantom.exit(1);
    return;
  }

  var result = page.evaluate(function () {
    var heading = document.querySelector('h1');
    if (heading) {
      heading.style.backgroundColor = 'yellow';
    }

    return {
      title: document.title,
      heading: heading ? heading.textContent.trim() : null,
      readyState: document.readyState
    };
  });

  console.log(JSON.stringify(result));
  phantom.exit();
});

Arguments and return values must cross the context boundary as JSON-compatible data. Pass strings, numbers, booleans, arrays, and plain objects. Do not return a DOM node, function, window object, or other browser-native object.

var page = require('webpage').create();
var selector = 'h1';

page.open('https://example.com', function (status) {
  if (status !== 'success') {
    phantom.exit(1);
    return;
  }

  var text = page.evaluate(function (cssSelector) {
    var node = document.querySelector(cssSelector);
    return node ? node.textContent.trim() : null;
  }, selector);

  console.log(text || 'Element not found');
  phantom.exit();
});

3. Wait for application-specific content

A load-finished callback can run before a single-page application finishes an API request or delayed render. If you know the condition that means the page is ready, poll for that condition with a bounded timeout.

var page = require('webpage').create();
var deadline;
var interval;
var timeoutMs = 10000;

function finish(code) {
  if (interval) {
    clearInterval(interval);
  }
  phantom.exit(code || 0);
}

page.open('https://example.com/dashboard', function (status) {
  if (status !== 'success') {
    console.error('Initial load failed: ' + status);
    finish(1);
    return;
  }

  deadline = Date.now() + timeoutMs;
  interval = setInterval(function () {
    var ready = page.evaluate(function () {
      var app = document.querySelector('[data-app-ready="true"]');
      return !!app;
    });

    if (ready) {
      console.log('Application content is ready');
      finish(0);
      return;
    }

    if (Date.now() >= deadline) {
      console.error('Timed out waiting for application content');
      finish(1);
    }
  }, 100);
});

Prefer an observable DOM marker or application signal. Use a delay only when no condition can be observed, and keep it bounded so a stalled page cannot leave the process running indefinitely.

var page = require('webpage').create();

page.open('https://example.com', function (status) {
  if (status !== 'success') {
    phantom.exit(1);
    return;
  }

  setTimeout(function () {
    var title = page.evaluate(function () {
      return document.title;
    });
    console.log(title);
    phantom.exit();
  }, 2000);
});

4. Register handlers before navigation

If code must be installed before the URL loads, use onInitialized. This is earlier than the load-finished callback and is useful for registering page listeners.

var page = require('webpage').create();

page.onInitialized = function () {
  page.evaluate(function () {
    document.addEventListener('DOMContentLoaded', function () {
      window.__domContentLoaded = true;
    });
  });
};

page.onLoadFinished = function (status) {
  if (status !== 'success') {
    phantom.exit(1);
    return;
  }

  var state = page.evaluate(function () {
    return {
      readyState: document.readyState,
      domContentLoaded: !!window.__domContentLoaded
    };
  });
  console.log(JSON.stringify(state));
  phantom.exit();
};

page.open('https://example.com');

5. Capture page console messages

Messages logged inside page.evaluate are not displayed in the PhantomJS process by default. Assign page.onConsoleMessage when you need browser-context logging.

var page = require('webpage').create();

page.onConsoleMessage = function (message, line, source) {
  console.log('[page] ' + source + ':' + line + ' ' + message);
};

page.open('https://example.com', function (status) {
  if (status !== 'success') {
    phantom.exit(1);
    return;
  }

  page.evaluate(function () {
    console.log('DOM inspection started');
  });

  phantom.exit();
});

6. Complete reusable script

This version accepts a URL, checks navigation, waits for an optional selector, extracts serializable data, and exits on every path.

var system = require('system');
var webpage = require('webpage');

if (system.args.length < 2) {
  console.error('Usage: phantomjs after-load.js URL [selector]');
  phantom.exit(2);
}

var url = system.args[1];
var selector = system.args[2] || null;
var page = webpage.create();
var timeoutMs = 10000;
var pollMs = 100;
var started = Date.now();
var timer;

function stop(code) {
  if (timer) {
    clearInterval(timer);
  }
  phantom.exit(code || 0);
}

function inspect() {
  return page.evaluate(function (cssSelector) {
    var node = cssSelector ? document.querySelector(cssSelector) : null;
    return {
      title: document.title,
      readyState: document.readyState,
      selectorFound: cssSelector ? !!node : true,
      text: node ? node.textContent.trim() : null
    };
  }, selector);
}

page.open(url, function (status) {
  if (status !== 'success') {
    console.error('Unable to load ' + url + ': ' + status);
    stop(1);
    return;
  }

  if (!selector) {
    console.log(JSON.stringify(inspect()));
    stop(0);
    return;
  }

  timer = setInterval(function () {
    var result = inspect();
    if (result.selectorFound) {
      console.log(JSON.stringify(result));
      stop(0);
    } else if (Date.now() - started >= timeoutMs) {
      console.error('Timed out waiting for selector: ' + selector);
      stop(1);
    }
  }, pollMs);
});

Run it with phantomjs after-load.js https://example.com h1.

7. Troubleshooting

Symptom Cause Fix
Callback status is fail PhantomJS detected a network error. Log the status, handle the error path, and verify the URL, DNS, TLS, redirects, and connectivity.
The process exits before JavaScript runs phantom.exit() was called before the callback or later timer completed. Move exit into the callback that owns the final asynchronous operation.
Dynamic content is missing Load completion happened before an application-specific request or render. Poll for a known selector or state marker, or use a bounded delay when no condition exists.
A DOM node cannot be printed or returned page.evaluate only transfers serializable values. Return text, attributes, numbers, booleans, or a plain object.
Page logs do not appear Page console output is not forwarded automatically. Set page.onConsoleMessage.
A selector wait never finishes The selector is wrong, content is behind an error state, or the app never renders it. Inspect the page, verify the selector, add a timeout, and report a nonzero exit code.
Code works in the outer script but not in evaluate The evaluated function runs in a separate page sandbox. Pass required values as arguments and keep PhantomJS orchestration outside the function.

8. Performance and reliability guidance

  • Do only the DOM work you need inside evaluate; return a small object instead of serializing a whole document.
  • Use one page instance for one navigation flow unless you have a deliberate reuse strategy.
  • Set explicit application readiness conditions and finite deadlines so delayed requests do not hang jobs.
  • Record the URL, load status, elapsed time, selector wait result, and process exit code for diagnosis.
  • Treat success as network-level load completion, not proof that business data is present.
  • When maintaining an existing PhantomJS system, verify behavior against the installed version because the referenced API pages are legacy documentation.

9. Or skip the browser setup

If the goal is a clean screenshot after a page has loaded, ScreenshotNeo provides a hosted capture API. Its request handles the browser lifecycle for you. See the ScreenshotNeo API documentation.

Hosted capture can remove common overlays before producing the image.
Hosted capture can remove common overlays before producing the image.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://example.com -o shot.webp
import requests
r = requests.get("https://api.screenshotneo.com/v1/shot", params={"access_key": "YOUR_API_KEY", "url": "https://example.com"}, timeout=90)
open("shot.webp", "wb").write(r.content)
const q = new URLSearchParams({ access_key: 'YOUR_API_KEY', url: 'https://example.com' });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);

ScreenshotNeo removes cookie and consent banners, newsletter popups, and chat widgets before the shot. Bot checks, blank pages, failed loads, timeouts, and cache hits are not billed, and response headers identify the page verdict and billing result. It also offers an MCP server with take_screenshot, get_page_info, and capture_pdf tools for Claude, Cursor, and other MCP clients. The free plan includes 1,000 screenshots per month without a card; paid plans start at $5 for 3,000 shots. Create a free ScreenshotNeo account.

10. FAQ

Does page.open wait for every JavaScript promise?

No. It reports PhantomJS’s load-finished event. Wait for an application-specific DOM condition when later code matters.

Can I call page.evaluate before page.open?

You can register setup from an earlier lifecycle hook such as onInitialized, but post-load DOM work belongs in the load callback or onLoadFinished.

Why must I call phantom.exit() myself?

PhantomJS scripts remain alive until they exit. Put the call after the last callback, timer, or polling loop you need.

What should a load callback return?

Use its status to choose success or failure handling. The callback itself does not need to return page data; obtain that data with page.evaluate.

Can evaluated code access PhantomJS variables?

Only values explicitly passed as arguments can cross into the page context, and results must be serializable on the way back.