Skip to main content

contextmanager for managing `selenium` chrome sessions with caching.

Project description

chromesession

PyPI - Python Version PyPI - Version PyPI - Downloads PyPI - License GitHub - Pytest GitHub - Page GitHub - Release codecov pre-commit


chromesession is a Python package that provides a convenient contextmanager for managing selenium chrome sessions.

In addition, a CachedSession is provided to directly cache the driver responses.

Installation

pip install chromesession

To use the chromesession.chrome context manager with selenium, the chromedriver must be installed on the system.

Alternatively, you can install the latest chromedriver as an extra.

pip install chromesession[driver]

Examples

Cache the specified URLs by fetching them via Selenium and saving the responses.

from pathlib import Path

from chromesession import CachedSession, chrome


def caching(*urls: str) -> Path:
    """
    Cache the specified URLs by fetching them via Selenium and saving the responses.
    """
    cachfile = "caching.sqlite"

    with CachedSession(cache_name=cachfile) as session:
        with chrome(verbose=False) as driver:
            for url in urls:
                if url in session:
                    print(f"{url=} already cached.")
                    continue

                try:
                    driver.get(url)
                    session.save_driver(driver)
                except Exception as e:
                    print(f"{url=} failed to cache: {e}", exc_info=True)
                else:
                    print(f"{url=} saved in cache.")

    return Path(cachfile)


if __name__ == "__main__":

    caching("https://example.com/", "https://example.com/")

Dependencies

PyPI - requests-cache PyPI - responses PyPI - selenium

Extras

Install optional dependencies using extras.

extra installation dependency
all pip install chromesession[all] Install all extras.
driver pip install chromesession[driver] PyPI - chromedrive-py
bs4 pip install chromesession[bs4] PyPI - beautifulsoup4

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

chromesession-0.1.1.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

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

chromesession-0.1.1-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file chromesession-0.1.1.tar.gz.

File metadata

  • Download URL: chromesession-0.1.1.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.13.2 Linux/6.8.0-1021-azure

File hashes

Hashes for chromesession-0.1.1.tar.gz
Algorithm Hash digest
SHA256 567be9810052fb6b4440df1016fdb2d8691c908f10787ea693fddd579afd0851
MD5 a97f760202eb31697033bc8623f1e834
BLAKE2b-256 89645c7bbbb21881e2a64f3b02cea456871064ca521f4a48a108b97f46778b2d

See more details on using hashes here.

File details

Details for the file chromesession-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: chromesession-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.13.2 Linux/6.8.0-1021-azure

File hashes

Hashes for chromesession-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 99fb4a93cf486d44bd8a723eafae198b22c6ab7f7ab27291de0521f6d48f347e
MD5 d8ab2c3c7f03b8d994fb55f91f407b9a
BLAKE2b-256 e865f7aa5ecb9a3723c4d8f6ee8b2ac86b1a8cdaba77e4c6de2ff7cd49a4d156

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