PhantomJS
Python
web scraping
automation
headless browser

Is there a way to use PhantomJS in Python?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Yes, Python can still drive PhantomJS, but only as a legacy workflow. PhantomJS is no longer maintained, so the more important answer today is that it is technically usable through command-line execution or older Selenium-based setups, but modern headless browsers such as Chrome, Firefox, and Playwright are usually better choices.

The Simplest Way: Run PhantomJS as a Subprocess

PhantomJS is a standalone executable that runs JavaScript files. Python can invoke it with subprocess, which is often the least fragile way to use it now.

Suppose you have a PhantomJS script named fetch_title.js:

javascript
1var system = require('system');
2var page = require('webpage').create();
3var url = system.args[1];
4
5page.open(url, function(status) {
6  if (status !== 'success') {
7    console.log('failed');
8    phantom.exit(1);
9  }
10
11  console.log(page.title);
12  phantom.exit();
13});

Then call it from Python:

python
1import subprocess
2
3result = subprocess.run(
4    ["phantomjs", "fetch_title.js", "https://example.com"],
5    capture_output=True,
6    text=True,
7    check=True,
8)
9
10print(result.stdout.strip())

This is real and runnable as long as the PhantomJS binary is installed and on your PATH.

Why Selenium Is No Longer the Best Story

Historically, many Python examples used PhantomJS through Selenium WebDriver. That worked when Selenium still exposed direct PhantomJS support, but the ecosystem moved on because PhantomJS stopped keeping up with modern browser standards and maintenance needs.

So if you see old Selenium examples, treat them as legacy documentation rather than as a recommended current setup.

If your real need is browser automation, use a maintained headless browser instead. For example, Playwright from Python is a modern replacement:

python
1from playwright.sync_api import sync_playwright
2
3with sync_playwright() as p:
4    browser = p.chromium.launch(headless=True)
5    page = browser.new_page()
6    page.goto("https://example.com")
7    print(page.title())
8    browser.close()

That is usually the right answer for new code.

When PhantomJS Still Makes Sense

There are still a few narrow situations where PhantomJS appears:

  • maintaining an old automation system
  • reproducing behavior in a legacy CI job
  • running scripts that already depend on PhantomJS-specific APIs

If that is your situation, the subprocess approach is usually simpler than fighting modern Selenium compatibility.

It also makes the dependency boundary obvious. Python launches a legacy browser process, waits for output, and treats the browser script as an external tool rather than as part of a modern browser-driver stack.

Practical Setup Advice

If you must keep PhantomJS around:

  • pin the binary version
  • keep the JavaScript automation scripts small and explicit
  • isolate the workflow from your newer browser automation stack
  • assume some modern sites will render incorrectly or incompletely

That last point matters. A lot of current web behavior depends on features that PhantomJS does not implement well anymore.

Common Pitfalls

  • Starting a new automation project on PhantomJS even though it is no longer maintained.
  • Expecting old Selenium plus PhantomJS examples to work unchanged in a modern Python environment.
  • Assuming a site that works in current Chrome will behave the same way in PhantomJS.
  • Treating PhantomJS as a long-term browser automation strategy instead of as a legacy compatibility tool.
  • Forgetting that the easiest Python integration path today is often just subprocess, not a browser-driver wrapper.

Summary

  • Python can still use PhantomJS, most reliably by invoking the PhantomJS binary through subprocess.
  • Old Selenium-based PhantomJS workflows are legacy patterns, not modern best practice.
  • PhantomJS is unmaintained, so compatibility with current websites is limited.
  • For new browser automation, use maintained tools such as Playwright or headless Chrome.
  • Keep PhantomJS only when you are supporting an older workflow that truly depends on it.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.