Skip to main content

A modern PhantomJS driver for Python with bundled binaries, providing headless browser automation, web page rendering, and JavaScript execution.

Project description

Phasma – Modern PhantomJS Driver for Python

Phasma Logo

Phasma is a Python library that provides a clean, high‑level interface to PhantomJS, enabling headless browser automation, web page rendering, and JavaScript execution. It simplifies the process of downloading, managing, and interacting with PhantomJS, making it ideal for web scraping, screenshot capture, and automated testing.

Features

  • Bundled Driver: PhantomJS driver is included in the package (Windows, macOS, Linux). No separate download needed.
  • Page Rendering: Render HTML files, strings, and remote URLs with JavaScript support.
  • JavaScript Execution: Run arbitrary JavaScript code in a PhantomJS context.
  • Direct PhantomJS Execution: Execute PhantomJS with full control over arguments, environment, and working directory.
  • SSL Control: Disable SSL verification by setting OPENSSL_CONF environment variable.
  • CLI Interface: Command‑line tools for quick operations.
  • Cross‑Platform: Works on Windows, Linux, and macOS.
  • Lightweight: Minimal dependencies, focused on simplicity and reliability.

Installation

Install from PyPI:

pip install phasma

The package includes the PhantomJS driver for Windows, macOS, and Linux (both 32‑bit and 64‑bit). No separate download is required.

Or install from source:

git clone https://github.com/MohammadRaziei/phantomjs-driver.git
cd phantomjs-driver
pip install -e .

Quick Start

Using the Python API

from phasma import render_page, render_url, execjs

# The PhantomJS driver is bundled with the package, ready to use.
# Render an HTML file
html = render_page("path/to/page.html")
print(html)

# Render a URL
content = render_url("https://example.com", wait_time=2000)
print(content[:500])  # first 500 characters

# Execute JavaScript
output = execjs("console.log('Hello from PhantomJS');")
print(output)

Using the Command Line

# Show driver version
python -m phasma driver --version

# Show driver executable path
python -m phasma driver --path

# Download the driver (use --force to re-download)
python -m phasma driver download
python -m phasma driver download --force

# Execute PhantomJS directly with arguments
python -m phasma driver exec script.js
python -m phasma driver exec --cwd /path/to/working/dir script.js
python -m phasma driver exec --ssl --timeout 10 script.js
python -m phasma driver exec --no-ssl --capture-output script.js

# Render an HTML file
python -m phasma render-page tests/data/test_page.html

# Render a URL
python -m phasma render-url https://example.com --wait 2000

# Execute JavaScript
python -m phasma execjs "console.log('Hello');"

API Reference

download_driver(os_name=None, arch=None, force=False)

Downloads the PhantomJS driver for the given OS and architecture. If no arguments are provided, it auto‑detects the current platform. Set force=True to re‑download even if the driver already exists.

Driver.exec(args, *, capture_output=False, timeout=30, check=False, ssl=False, env=None, cwd=None, **kwargs)

Executes PhantomJS with the given arguments. Returns a subprocess.CompletedProcess instance.

  • args: Command line arguments as a string or sequence of strings.
  • capture_output: If True, capture stdout and stderr.
  • timeout: Timeout in seconds.
  • check: If True, raise CalledProcessError on non‑zero exit code.
  • ssl: If False (default), set OPENSSL_CONF environment variable to empty string (disables SSL verification).
  • env: Optional environment variables dictionary for subprocess.
  • cwd: Optional working directory for subprocess.

render_page(page, output=None, viewport_size="1024x768", wait_time=100, **kwargs)

Renders an HTML page (file path or HTML string) and returns the rendered HTML. Optionally saves the output to a file.

render_url(url, output=None, viewport_size="1024x768", wait_time=0, **kwargs)

Renders a remote URL and returns the rendered HTML. Optionally saves the output to a file.

execjs(script, args=None, **kwargs)

Executes JavaScript code in a PhantomJS context and returns the stdout.

Advanced Usage

Rendering Dynamic JavaScript Content

Phasma can render pages that modify their DOM with JavaScript:

from phasma import render_page

html_with_js = """
<html>
<body>
    <div id="container">Initial</div>
    <script>
        document.getElementById('container').innerHTML = '<h2>Generated by JS</h2>';
    </script>
</body>
</html>
"""

rendered = render_page(html_with_js, wait_time=500)
assert "<h2>Generated by JS</h2>" in rendered

Custom Viewport and Wait Time

# Render with a custom viewport and longer wait
rendered = render_page(
    "page.html",
    viewport_size="1920x1080",
    wait_time=2000,
    output="screenshot.html"
)

Testing

Run the test suite with pytest:

pytest tests/

All core functionality is covered by unit tests, including page rendering, URL fetching, and JavaScript execution.

Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.

  1. Fork the repository.
  2. Create a feature branch.
  3. Make your changes and add tests.
  4. Ensure all tests pass.
  5. Submit a pull request.

License

Phasma is distributed under the MIT License. See the LICENSE file for details.

Acknowledgments

  • PhantomJS team for the amazing headless browser.
  • The Python community for excellent tooling and support.

Phasma – Making PhantomJS easy for Python developers.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

phasma-0.4.1-py3-none-win_amd64.whl (18.2 MB view details)

Uploaded Python 3Windows x86-64

phasma-0.4.1-py3-none-manylinux_2_17_x86_64.whl (26.2 MB view details)

Uploaded Python 3manylinux: glibc 2.17+ x86-64

phasma-0.4.1-py3-none-manylinux_2_17_i686.whl (27.3 MB view details)

Uploaded Python 3manylinux: glibc 2.17+ i686

phasma-0.4.1-py3-none-macosx_10_9_universal2.whl (17.2 MB view details)

Uploaded Python 3macOS 10.9+ universal2 (ARM64, x86-64)

File details

Details for the file phasma-0.4.1-py3-none-win_amd64.whl.

File metadata

  • Download URL: phasma-0.4.1-py3-none-win_amd64.whl
  • Upload date:
  • Size: 18.2 MB
  • Tags: Python 3, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for phasma-0.4.1-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 40b261ca3f4b780d50d3445b98893cba7e4dbf52110df9dd5620c85634931154
MD5 718e5e8019ccd934492c567cd6ce6101
BLAKE2b-256 0cd8e876c48503243f01da193938e9447be745e2031dc0b17932ec78d40c59be

See more details on using hashes here.

File details

Details for the file phasma-0.4.1-py3-none-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for phasma-0.4.1-py3-none-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 6120cf2f9025df4d1bc19c92a0c1db985ae095f798ad897734e2b75665b23a26
MD5 a152c981300670f52924bfb11c52ab66
BLAKE2b-256 03c98f0228a373002238b64c3291544e6c1fd37bc7960759e03ceba457d65ce4

See more details on using hashes here.

File details

Details for the file phasma-0.4.1-py3-none-manylinux_2_17_i686.whl.

File metadata

File hashes

Hashes for phasma-0.4.1-py3-none-manylinux_2_17_i686.whl
Algorithm Hash digest
SHA256 177320523906fe023422a3380c7bf321257e20b190d1902c8ed8589c79281c31
MD5 eb709265b6925600ab988615423b2e51
BLAKE2b-256 eec0e6745cacb07a6e79767009373b0b315dc27c43f9a0fc792c7bf8f93c1756

See more details on using hashes here.

File details

Details for the file phasma-0.4.1-py3-none-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for phasma-0.4.1-py3-none-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 d4551a3bcaab1be55f7efcfe347bb3e8d9c74145b521ef4a27610a4d0051f9ab
MD5 56e51f8174fc67e71c9206da4dac459d
BLAKE2b-256 18210e526c792f0de9beb42407fd5b4016de515b0075dc1f997e9ba407f85913

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page