Skip to main content

No project description provided

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.0-py3-none-win_amd64.whl (18.2 MB view details)

Uploaded Python 3Windows x86-64

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

Uploaded Python 3manylinux: glibc 2.17+ x86-64

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

Uploaded Python 3manylinux: glibc 2.17+ i686

phasma-0.4.0-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.0-py3-none-win_amd64.whl.

File metadata

  • Download URL: phasma-0.4.0-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.0-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 c22fd07a7ceaa3f56b9ee2d6eee149880aeec4a1124b6767c281331057db7aa4
MD5 d0440beca367532aa191ccad634554e2
BLAKE2b-256 a7b542a041dff0b257a943681f416ec587ff7fc6a6f7910fc33580cb3e8ead8f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for phasma-0.4.0-py3-none-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 5a38302acdfee157ac491a6b59e066e2758355227bcc6b2da51180949f209bc2
MD5 3d60b7ef88f692f08373ea25ef8db4bf
BLAKE2b-256 13ba148915aa39d99b891940e73b9e906795864b0ac9e5b5337701529d8a17dd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for phasma-0.4.0-py3-none-manylinux_2_17_i686.whl
Algorithm Hash digest
SHA256 c78c1a1426ce76272f35a69a385dce6a49aca91143d49a1c590c540350db3243
MD5 ef2d627065b4dec67e11910833ff374d
BLAKE2b-256 6cd52b4306c4a77d0e426c70ad02d7674850c31c7fab10d6ce81920203fe173b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for phasma-0.4.0-py3-none-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 84e7bb47451b2ea85d7b642d3f735a364faa29d9b9bce11545e6ec739daed394
MD5 f5f1f3d0fefa4ea212f18b8055638ab5
BLAKE2b-256 0a22e36a37dee340267c9431ba9e0e9f506ac7938b0b37a5764e567471711621

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