Metadata-Version: 2.4
Name: smart-webdriver-manager
Version: 0.10.9
Summary: A smart webdriver and browser manager
License: MIT
License-File: LICENSE
Author: bissli
Requires-Python: >=3.10,<4.0
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Natural Language :: English
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Topic :: Software Development :: Libraries
Provides-Extra: test
Requires-Dist: asserts ; extra == "test"
Requires-Dist: backoff
Requires-Dist: bump2version ; extra == "test"
Requires-Dist: filelock
Requires-Dist: mock (>=4.0.3,<5.0.0) ; extra == "test"
Requires-Dist: packaging
Requires-Dist: platformdirs
Requires-Dist: pytest ; extra == "test"
Requires-Dist: pytest-cov ; extra == "test"
Requires-Dist: requests
Requires-Dist: selenium (>=4.1.0,<5.0.0) ; extra == "test"
Requires-Dist: tqdm
Requires-Dist: urllib3 (>=2)
Project-URL: Repository, https://github.com/bissli/smart-webdriver-manager
Description-Content-Type: text/markdown

Smart Webdriver Manager
=======================
[![PyPI](https://img.shields.io/pypi/v/smart-webdriver-manager.svg)](https://pypi.org/project/smart-webdriver-manager)
[![Supported Python Versions](https://img.shields.io/pypi/pyversions/smart-webdriver-manager.svg)](https://pypi.org/project/smart-webdriver-manager/)

A smart webdriver manager. Inspired by [webdriver_manager](https://github.com/SergeyPirogov/webdriver_manager/) and [chromedriver-binary-auto](https://github.com/danielkaiser/python-chromedriver-binary).

Unlike other managers, this module manages the driver, browser, and data directory independently of the system installed browser.

The manager requires only a specified browser version (i.e. Chromium 117, 118, 120, etc.). It manages the remaining components.

These are then cached for future use in the user-specified or system-default directory. In other words:

```python
for version in [0, 75, 80, 95, 96]: # 0 -> latest
  # auto-fetch and configure all the needed components and run the automation
```


Supported Driver/Platform
--------------------------

|                           | **Chromium** | **Firefox** |
| ------------------------- | ------------ | ----------- |
| **Windows**               | x            | -           |
| **Linux (x86-64)**        | x            | -           |
| **macOS (Apple Silicon)** | x            | -           |

Installation
------------

```bash
pip install smart-webdriver-manager
```

Examples
--------

### Basic Usage

```python
import os
from selenium import webdriver
from selenium.webdriver import ChromeOptions
from selenium.webdriver.chrome.service import Service
from smart_webdriver_manager import ChromeDriverManager

version = os.getenv('MY_ENVIRONMENTAL_VARIABLE')
cdm = ChromeDriverManager(version=version)

driver_path = cdm.get_driver()
browser_path = cdm.get_browser()
user_data_path = cdm.get_browser_user_data()

options = ChromeOptions()
options.binary_location = browser_path
options.add_argument(f'--user-data-dir={user_data_path}')
service = Service(executable_path=driver_path)
driver = webdriver.Chrome(service=service, options=options)
try:
    driver.get("http://google.com")
finally:
    driver.quit()
```

The components are modular. `get_driver()` downloads only chromedriver, and `get_browser()` downloads only Chromium.
One manager pairs both with the same driver release. Other modules may suit a project that needs only a driver.

Updating automation takes little work. When a new version breaks the automation, an earlier version number restores it.
Verifying a version needs no browser install or uninstall.

### Multiple Versions

```python
from smart_webdriver_manager import ChromeDriverManager

for version in [117, 118, 120, 0]:  # 0 -> latest
    cdm = ChromeDriverManager(version=version)
    driver_path = cdm.get_driver()
    browser_path = cdm.get_browser()
    # run automation with this version ...
```

### Custom Cache Directory

```python
from smart_webdriver_manager import ChromeDriverManager

cdm = ChromeDriverManager(version=120, base_path='/tmp/my-cache')
driver_path = cdm.get_driver()
```

### Cache Management

```python
from smart_webdriver_manager import ChromeDriverManager

cdm = ChromeDriverManager(version=120)

# Remove cached driver for this version
cdm.remove_driver()

# Remove cached browser for this version
cdm.remove_browser()

# Remove user data directory for this version
cdm.remove_browser_user_data()

# Remove all cached drivers and browsers, keeping every user data directory
cdm.clear_cache()

# Remove all cached drivers and browsers, and every user data directory
cdm.clear_cache(clear_profile=True)
```

`clear_cache()` removes every driver and browser the cache metadata records, one entry at a time under that entry's lock.
An unpack in progress in another process finishes first, and an entry not yet recorded stays in the cache.

`clear_cache()` keeps the user data directory (`UserData`) of every browser release, with its cookies and logins.
Only `remove_browser_user_data()` and `clear_cache(clear_profile=True)` remove a user data directory.
The first removes the directory of the current version's release. The second removes the directory of every release.

macOS Notes
-----------

When running Chromium on macOS, the browser may trigger a **"Do you want to allow this app to find devices on your local network?"** popup. The following Chrome option suppresses it:

```python
options.add_argument('--disable-features=MediaRouter')
```

API Reference
-------------

### `ChromeDriverManager(version=None, base_path=None)`

| Parameter   | Type                    | Default | Description                                                     |
| ----------- | ----------------------- | ------- | --------------------------------------------------------------- |
| `version`   | `int`, `str` or `None`  | `None`  | Browser version (e.g. 117, '120'). `None`, `0` or `''` = latest |
| `base_path` | `str`, `Path` or `None` | `None`  | Cache directory. `None` = system default                        |

### Methods

| Method                             | Returns | Description                                                                                         |
| ---------------------------------- | ------- | --------------------------------------------------------------------------------------------------- |
| `get_driver()`                     | `str`   | Download/cache chromedriver and return its path                                                     |
| `get_browser()`                    | `str`   | Download/cache Chromium browser and return its path                                                 |
| `get_browser_user_data()`          | `str`   | Return path to user data directory for this version                                                 |
| `remove_driver()`                  | `None`  | Remove cached driver for the current version                                                        |
| `remove_browser()`                 | `None`  | Remove cached browser for the current version                                                       |
| `remove_browser_user_data()`       | `None`  | Remove user data directory for the current version                                                  |
| `clear_cache(clear_profile=False)` | `None`  | Remove all cached drivers and browsers. `clear_profile=True` also removes every user data directory |

Technical Layout
----------------

Some module definitions:

- `Version`: main browser version, ie Chrome 95
- `Release`: subversion: ie Chrome 95.01.1121
- `Revision`: browser-only, snapshots within a release

To clarify how the module works, below is the cache directory illustrated:

1. For a requested version, the driver is the newest cached release of that version, or the latest published release when none is cached. For the latest version, the manager asks for the latest stable release on every new instance.
2. The browser is the newest Chromium snapshot revision at or below the branch position of the version's milestone. Its directory sits under the driver's release.
3. A user data directory belongs to a release and sits beside the release's revision directories.

For example, with the cache below, a request for version 96 returns driver 96.1.85.111 and browser revision 929512.

```
swm/
    browsers/
        chrome/ [linux]
            96.1.85.111/
                929511/
                    chrome-linux/
                        chrome
                        ...
                929512/
                    chrome-linux/
                        chrome
                        ...
                UserData/
                    ...
        chrome/ [macOS]
            125.0.6422.141/
                1287718/
                    chrome-mac/
                        Chromium.app/
                            Contents/MacOS/Chromium
                UserData/
                    ...
    drivers/
        chromedriver/ [windows]
            96.1.85.54/
                chromedriver.exe
            96.1.85.111/
                chromedriver.exe
    browsers.json
    drivers.json
```

The system default directory for the cache is as follows:

- `Windows`: ~/AppData/Roaming/swm/swm
- `Linux`:   ~/.local/share/swm
- `macOS`:   ~/Library/Application Support/swm

Development
-----------

There are two ways to run local tests

```bash
pip install -U pip poetry tox
git clone https://github.com/bissli/smart-webdriver-manager.git && cd smart-webdriver-manager
tox
```

```bash
pip install -U pip poetry
git clone https://github.com/bissli/smart-webdriver-manager.git && cd smart-webdriver-manager
poetry install --extras test
poetry shell
pytest
```

Contributing
------------

Active contributions are welcome (collaboration). A PR is the route for a feature or a bug fix.

