Skip to content

d-chris/chromesession

Repository files navigation

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

About

contextmanager for managing `selenium` chrome sessions with caching.

Topics

Resources

License

Stars

Watchers

Forks