https://github.com/d-chris/chromesession
contextmanager for managing `selenium` chrome sessions with caching.
https://github.com/d-chris/chromesession
beautifulsoup4 chromedriver-py python requests-cache responses selenium
Last synced: 4 months ago
JSON representation
contextmanager for managing `selenium` chrome sessions with caching.
- Host: GitHub
- URL: https://github.com/d-chris/chromesession
- Owner: d-chris
- License: mit
- Created: 2025-03-13T17:16:38.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-12-19T05:04:13.000Z (6 months ago)
- Last Synced: 2026-01-05T06:23:19.103Z (6 months ago)
- Topics: beautifulsoup4, chromedriver-py, python, requests-cache, responses, selenium
- Language: Python
- Homepage: https://d-chris.github.io/chromesession/
- Size: 278 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# chromesession
[](https://pypi.org/project/chromesession/)
[](https://pypi.org/project/chromesession/)
[](https://pypi.org/project/chromesession/)
[](https://raw.githubusercontent.com/d-chris/chromesession/main/LICENSE)
[](https://github.com/d-chris/chromesession/actions/workflows/pytest.yml)
[](https://d-chris.github.io/chromesession)
[](https://github.com/d-chris/chromesession)
[](https://codecov.io/gh/d-chris/chromesession)
[](https://raw.githubusercontent.com/d-chris/chromesession/main/.pre-commit-config.yaml)
---
`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
```cmd
pip install chromesession
```
To use the `chromesession.chrome` context manager with `selenium`, the [chromedriver](https://googlechromelabs.github.io/chrome-for-testing/) must be installed on the system.
Alternatively, you can install the latest `chromedriver` as an [extra](#extras).
```cmd
pip install chromesession[driver]
```
## Examples
Cache the specified URLs by fetching them via Selenium and saving the responses.
```python
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
[](https://pypi.org/project/requests-cache/)
[](https://pypi.org/project/responses/)
[](https://pypi.org/project/selenium/)
## Extras
Install optional dependencies using extras.
| extra | installation | dependency |
| ------ | ----------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| all | `pip install chromesession[all]` | Install all extras. |
| driver | `pip install chromesession[driver]` | [](https://pypi.org/project/chromedriver-py/) |
| bs4 | `pip install chromesession[bs4]` | [](https://pypi.org/project/beautifulsoup4/) |
---