A Python module for managing Selenium Chrome sessions across multiple Docker containers.
Project description
Docker Chrome Session Manager
A Python module for managing Selenium Chrome sessions across multiple Docker containers. It provides dynamic container selection, session configuration persistence, and safe concurrent access for browser automation tasks.
Features
- Dynamic Container Management: Automatically selects available Selenium Chrome containers from a provided list.
- Session Configuration: Supports customizable browser settings such as user agent, locale, screen resolution, timezone, and additional Chrome options.
- Persistent Sessions: Stores session configurations in JSON files for reuse across sessions.
- Concurrent Access: Uses file locking to ensure safe access to session configurations.
- Logging: Comprehensive logging for debugging and monitoring session activities.
- Extensible: Easily integrates with Dockerized Selenium Chrome instances and supports custom configurations.
Installation
Prerequisites
- Python 3.9 or higher
- Docker and Docker Compose
- Poetry (optional, for dependency management)
Steps
Direct installation
Just use pip:
pip install docker-chrome-session-manager
Or Poetry:
poetry add docker-chrome-session-manager
Manual installation
-
Clone the repository:
git clone https://github.com/FINWAX/docker-chrome-session-manager.py.git cd docker-chrome-session-manager
-
Install dependencies: Using Poetry (recommended):
poetry installAlternatively, using pip:
pip install -r requirements.txt
-
Set up Docker containers: Start the Selenium Chrome containers using the provided
docker-compose.yml:docker-compose up -d
This will start two Selenium Chrome containers on ports
4444and4445, with session data stored in the./sessionsdirectory.
Usage
The docker_chrome_session_manager module provides a SessionManager class to manage browser sessions. Below is an
example of how to use it:
import logging
from docker_chrome_session_manager import SessionManager
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
# Initialize SessionManager with container URLs
container_paths = ["http://localhost:4444", "http://localhost:4445"]
manager = SessionManager(container_paths=container_paths)
# Create a session configuration
resource_id = "test-session-123"
config = manager.provide_session_config(
resource_id=resource_id,
locale="en-US",
timezone="America/New_York",
extra_chrome_options=["--disable-notifications"]
)
# Start a browser session
driver = manager.get_remote_driver(resource_id, config)
if driver:
try:
driver.get("https://example.com")
print(f"Page title: {driver.title}")
finally:
driver.quit()
manager.forget_session_config(resource_id) # Clean up session
See the example/example.py file for a more detailed example that demonstrates multiple sessions with different
configurations.
Configuration
The SessionManagerConfig dataclass provides global settings for the SessionManager. Key parameters include:
config_dir: Directory for storing session configuration files (default:config/sessions).session_dir: Directory for browser session data (default:/sessionsin Docker containers).screen_resolutions: Available screen resolutions (e.g.,(1920, 1080),(1280, 720)).default_locale: Default browser locale (e.g.,en-US).user_agent_platforms: Platforms for generating user agents (e.g.,desktop).file_lock_timeout: Timeout for file locking (default: 10 seconds).container_response_timeout: Timeout for container status checks (default: 3 seconds).
The SessionConfig dataclass allows customization of individual sessions, including:
user_agent: Custom user agent string.locale: Browser locale (e.g.,en-US,ru-RU).resolution: Screen resolution as a tuple (e.g.,(1920, 1080)).timezone: Timezone ID (e.g.,America/New_York).extra_chrome_options: Additional Chrome command-line options.
Running the Example
To run the example script provided in example/example.py:
-
Ensure Docker containers are running:
docker-compose -f example/docker-compose.yml up -d
-
Run the example script:
poetry run python example/example.py
or, if using pip:
python example/example.py
The script will create two browser sessions with different configurations, navigate to specified URLs, and log session details.
License
This project is licensed under the MIT License. See the LICENSE file for details.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file docker_chrome_session_manager-1.0.3.tar.gz.
File metadata
- Download URL: docker_chrome_session_manager-1.0.3.tar.gz
- Upload date:
- Size: 7.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.10.11 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ae0188f88690bac04ce95dc73d04f0b2a102226ddd1ceb1e31a99b7f81a57a32
|
|
| MD5 |
4c84b826dafebef84074cc5426901db0
|
|
| BLAKE2b-256 |
c22b36677b9f8509bfebc99b2e85a78ce6d127c629527866d10e60b6c4d716d4
|
File details
Details for the file docker_chrome_session_manager-1.0.3-py3-none-any.whl.
File metadata
- Download URL: docker_chrome_session_manager-1.0.3-py3-none-any.whl
- Upload date:
- Size: 9.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.10.11 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e47148ed0cd46474795ad0b28356e817c4a71a57caa8a779c10d4a34cd98b829
|
|
| MD5 |
56b2084f0b2b52873d6a86e0c3e23f58
|
|
| BLAKE2b-256 |
08e8b7c9faa2fa049aae56b683f48a429f676e1f8fd3854b70b57f51eea2b808
|