Skip to main content

No project description provided

Project description

Phasma: 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.
  • 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
python -m phasma driver download

# 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)

Downloads the PhantomJS driver for the given OS and architecture. If no arguments are provided, it auto‑detects the current platform.

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

Uploaded Python 3Windows x86-64

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

Uploaded Python 3manylinux: glibc 2.17+ x86-64

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

Uploaded Python 3manylinux: glibc 2.17+ i686

phasma-0.1.0-py3-none-macosx_10_9_x86_64.whl (17.2 MB view details)

Uploaded Python 3macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: phasma-0.1.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.1.0-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 50f98af96d6314a26978cbeb9f618a6365b0246a68c60b8d7861f0a75555004f
MD5 e2fd15776dec383b1acd7a4abbdb3d7f
BLAKE2b-256 0d062d88959641ff4ba5ad36a9fcfbb2acf8b73600b73c519a9739604cf2bb25

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for phasma-0.1.0-py3-none-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 b13505bba12276f684487432087d748bde15d7130c3d3d4914c98f5f9b166601
MD5 8321c9c213beda26096101dfd57bb2d4
BLAKE2b-256 ee71c1c5340b26467ae30f8d4b3ad2ebbc1fb97f78a2d92c4d7de0e47c11a9ac

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for phasma-0.1.0-py3-none-manylinux_2_17_i686.whl
Algorithm Hash digest
SHA256 16a90387c9f7eb1267d620cda54a55981b820c6dc0d9dfff461ae50f486033a2
MD5 4911ae343f312e11efff0958935cba62
BLAKE2b-256 9dd17cd6db849324e29b2205292aaf88e70ace22551bcf7646e531598829a6af

See more details on using hashes here.

File details

Details for the file phasma-0.1.0-py3-none-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for phasma-0.1.0-py3-none-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 86a7e71ab084c4e02aebc2eab25d3ab62cd0c731899448aa610dbdedc1305e01
MD5 50eb073fdf3126b4a07f80fb61019b5a
BLAKE2b-256 d6d930401bcfddadcb0a49a1f5d04b3aca1783ad96ca6a9f602dcfdfe310832d

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