Skip to main content

A robust and extensible Chrome browser automation tool

Project description

Chrome Puppet

Automate Chrome with confidence - A robust, production-ready browser automation framework

PyPI Python Version Tests Codecov License: MIT Code style: black

Chrome Puppet is a Python framework that makes browser automation simple and reliable. Built on top of Selenium, it provides a clean, intuitive API for automating Chrome/Chromium browsers with built-in best practices for stability and maintainability.

โœจ Features

  • Modern API: Intuitive Python interface for browser control
  • Cross-Platform: Works on Windows, macOS, and Linux
  • Robust Error Handling: Comprehensive error handling and recovery
  • Automatic ChromeDriver Management: No need to manually manage ChromeDriver versions
  • Headless Mode: Run browsers in headless mode for CI/CD pipelines
  • Extensible: Easy to extend with custom functionality

๐Ÿš€ Quick Start

Prerequisites

  • Python 3.8+
  • Chrome/Chromium browser

Installation

# Install from PyPI
pip install chrome-puppet

# Or install from source
git clone https://github.com/consumrbuzzy/chrome-puppet.git
cd chrome-puppet
pip install -e .

Basic Usage

from chrome_puppet import ChromePuppet

# Create a browser instance and navigate to a page
with ChromePuppet() as browser:
    browser.get("https://example.com")
    print(f"Page title: {browser.title}")

For more detailed examples, see EXAMPLES.md.

๐Ÿ›  Project Structure

chrome-puppet/
โ”œโ”€โ”€ core/                    # Core browser automation code
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ browser/             # Browser implementation
โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”‚   โ”œโ”€โ”€ base.py          # Base browser class
โ”‚   โ”‚   โ”œโ”€โ”€ chrome.py        # Chrome implementation
โ”‚   โ”‚   โ”œโ”€โ”€ element.py       # Element interactions
โ”‚   โ”‚   โ”œโ”€โ”€ exceptions.py    # Custom exceptions
โ”‚   โ”‚   โ”œโ”€โ”€ navigation.py    # Navigation utilities
โ”‚   โ”‚   โ””โ”€โ”€ screenshot.py    # Screenshot functionality
โ”‚   โ”œโ”€โ”€ config.py            # Configuration management
โ”‚   โ””โ”€โ”€ utils/               # Utility functions
โ”œโ”€โ”€ tests/                   # Test suite
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ base_test.py         # Base test class
โ”‚   โ”œโ”€โ”€ conftest.py          # Pytest configuration
โ”‚   โ”œโ”€โ”€ test_data/           # Test data files
โ”‚   โ””โ”€โ”€ test_browser.py      # Browser automation tests
โ”œโ”€โ”€ examples/                # Example scripts
โ”‚   โ””โ”€โ”€ browser_example.py   # Example usage
โ”œโ”€โ”€ .gitignore
โ”œโ”€โ”€ CHANGELOG.md
โ”œโ”€โ”€ EXAMPLES.md
โ”œโ”€โ”€ LICENSE
โ”œโ”€โ”€ pyproject.toml
โ”œโ”€โ”€ README.md
โ”œโ”€โ”€ requirements.txt         # Runtime dependencies
โ””โ”€โ”€ requirements-dev.txt     # Development dependencies

๐Ÿ“š Documentation

๐Ÿงช Running Tests

# Install test dependencies
pip install -r requirements-dev.txt

# Run all tests
pytest tests/

# Run with coverage report
pytest --cov=core tests/

๐Ÿค Contributing

Contributions are welcome! Please read our Contributing Guidelines for details on how to submit pull requests.

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ“ฆ Installation

  1. Clone the repository:

    git clone https://github.com/yourusername/chrome-puppet.git
    cd chrome-puppet
    
  2. Set up a virtual environment (recommended):

    python -m venv venv
    # On Windows:
    .\venv\Scripts\Activate.ps1
    # On macOS/Linux:
    # source venv/bin/activate
    
  3. Install dependencies:

    pip install -e .
    

    For development:

    pip install -r requirements-dev.txt
    

macOS/Linux

# Create virtual environment
python3 -m venv .venv

# Activate the virtual environment
source .venv/bin/activate

# Install dependencies
pip install -r requirements.txt

# For development
pip install -r requirements-dev.txt

Using the Virtual Environment

  • To activate the virtual environment, run the appropriate command above
  • Your command prompt should show (.venv) at the beginning when activated
  • To deactivate, simply type deactivate
  • Always activate the virtual environment before running the project

Environment Variables

Create a .env file in the project root based on .env.example:

# Chrome settings
CHROME_HEADLESS=false
CHROME_WINDOW_SIZE=1920,1080
CHROME_IMPLICIT_WAIT=10

# Logging
LOG_LEVEL=INFO
LOG_FILE=chrome_puppet.log

# Browser settings
CHROME_PATH=auto  # Set to 'auto' for auto-detection or specify path
CHROME_VERSION_OVERRIDE=  # Leave empty for auto-detection

Quick Start

Basic Usage

from chrome_puppet import ChromePuppet

# Initialize Chrome Puppet (runs in headless mode by default)
with ChromePuppet() as browser:
    # Navigate to a website
    browser.get("https://www.example.com")
    
    # Get page title
    print(f"Page title: {browser.title}")
    
    # Take a screenshot (saved to screenshots/ directory)
    screenshot_path = browser.take_screenshot("example_page")
    print(f"Screenshot saved to: {screenshot_path}")

Advanced Configuration

from chrome_puppet import ChromePuppet, ChromeConfig

# Create a custom configuration
config = ChromeConfig(
    headless=False,  # Run in visible mode
    window_size=(1366, 768),
    user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
    download_dir="path/to/downloads",
    implicit_wait=10,  # seconds
    verbose=True  # Enable debug logging
)

# Use the custom configuration
with ChromePuppet(config=config) as browser:
    browser.get("https://www.example.com")
    print(f"Current URL: {browser.driver.current_url}")

Configuration

The ChromeConfig class allows you to customize the browser behavior:

config = ChromeConfig(
    headless=True,  # Run in headless mode
    window_size=(1920, 1080),  # Browser window size
    user_agent="Custom User Agent String",
    timeout=30,  # Default timeout in seconds
    chrome_type=ChromeType.GOOGLE,  # Or ChromeType.CHROMIUM
    download_dir="./downloads",  # Custom download directory
    chrome_arguments=[
        "--disable-notifications",
        "--disable-infobars"
    ]
)

Examples

Basic Usage

from chrome_puppet import ChromePuppet

with ChromePuppet() as browser:
    browser.get("https://quotes.toscrape.com/")
    soup = browser.get_soup()
    quotes = soup.find_all('div', class_='quote')
    for quote in quotes:
        print(quote.find('span', class_='text').text)

Handling Dynamic Content

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

with ChromePuppet() as browser:
    browser.get("https://example.com/dynamic")
    
    # Wait for an element to be present
    element = WebDriverWait(browser.driver, 10).until(
        EC.presence_of_element_located((By.ID, "dynamic-element"))
    )
    
    # Interact with the element
    element.click()

Project Structure

stable_chrome_puppet/
โ”œโ”€โ”€ __init__.py           # Package initialization
โ”œโ”€โ”€ chrome.py            # Main Chrome browser implementation
โ”œโ”€โ”€ config.py             # Configuration classes
โ”œโ”€โ”€ utils.py              # Utility functions
โ”œโ”€โ”€ example.py            # Example usage
โ”œโ”€โ”€ requirements.txt      # Project dependencies
โ””โ”€โ”€ README.md            # This file

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License.

Project details


Download files

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

Source Distribution

chrome_puppet-0.1.0.tar.gz (37.0 kB view details)

Uploaded Source

Built Distribution

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

chrome_puppet-0.1.0-py3-none-any.whl (42.7 kB view details)

Uploaded Python 3

File details

Details for the file chrome_puppet-0.1.0.tar.gz.

File metadata

  • Download URL: chrome_puppet-0.1.0.tar.gz
  • Upload date:
  • Size: 37.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for chrome_puppet-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c3055130d94fd3f49a18808db899a4f8259c2d761137c36940c9a06ae9fc308c
MD5 3952343394bc5668e5e14f5a60d76630
BLAKE2b-256 5e0be7d888a0f64586443407060b82d81d12216567fb5e77fcef9df6c7a6b560

See more details on using hashes here.

File details

Details for the file chrome_puppet-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: chrome_puppet-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 42.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for chrome_puppet-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 17af55e711a1a51d060df992990892bf5974febf06ae25dd405e1b47afcaa439
MD5 7e6a436999c74a430f3330f1b62c428e
BLAKE2b-256 b4c823cee3be5df0161aae8523e359c4f4117438109c400c9f9c83933e5227f4

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