Use this file to discover all available pages before exploring further.
Nodriver provides comprehensive methods for managing browser cookies and storage. This is essential for maintaining sessions, testing authentication flows, and handling state.
# Get cookies as CDP objectscookies = await browser.cookies.get_all()for cookie in cookies: print(f"{cookie.name}: {cookie.value}") print(f" Domain: {cookie.domain}") print(f" Path: {cookie.path}") print(f" Secure: {cookie.secure}")
From browser.py:734-769:
async def get_all( self, requests_cookie_format: bool = False) -> List[Union[cdp.network.Cookie, "http.cookiejar.Cookie"]]: """ Get all cookies :param requests_cookie_format: when True, returns python http.cookiejar.Cookie objects, compatible with requests library and many others. """ connection = None for tab in self._browser.tabs: if tab.closed: continue connection = tab break else: connection = self._browser.connection cookies = await connection.send(cdp.storage.get_cookies()) if requests_cookie_format: import requests.cookies return [ requests.cookies.create_cookie( name=c.name, value=c.value, domain=c.domain, path=c.path, expires=c.expires, secure=c.secure, ) for c in cookies ] return cookies
Get cookies compatible with the Python requests library:
import requests# Get cookies in requests formatcookies = await browser.cookies.get_all(requests_cookie_format=True)# Use with requests libraryresponse = requests.get('https://api.example.com', cookies=cookies)
This is useful when you need to transfer cookies from a browser session to API calls made with requests.
async def clear(self): """ Clear current cookies Note: this includes all open tabs/windows for this browser """ connection = None for tab in self._browser.tabs: if tab.closed: continue connection = tab break else: connection = self._browser.connection await connection.send(cdp.storage.clear_cookies())
Clearing cookies affects all tabs and windows in the browser instance.
# Save all cookiesawait browser.cookies.save(file='session.dat')# Save cookies matching a patternawait browser.cookies.save( file='cloudflare.dat', pattern='(cf|cloudflare)')# Save .com cookiesawait browser.cookies.save( file='dotcom.dat', pattern='\.com')
From browser.py:791-834:
async def save(self, file: PathLike = ".session.dat", pattern: str = ".*"): """ Save all cookies (or a subset, controlled by `pattern`) to a file to be restored later :param file: path to save file :param pattern: regex style pattern string. Any cookie that has a domain, key or value field which matches the pattern will be included. default = ".*" (all) eg: the pattern "(cf|.com|nowsecure)" will include those cookies which: - have a string "cf" (cloudflare) - have ".com" in them, in either domain, key or value field. - contain "nowsecure" """ import re pattern = re.compile(pattern) save_path = pathlib.Path(file).resolve() connection = None for tab in self._browser.tabs: if tab.closed: continue connection = tab break else: connection = self._browser.connection cookies = await self.get_all(requests_cookie_format=False) included_cookies = [] for cookie in cookies: for match in pattern.finditer(str(cookie.__dict__)): logger.debug( "saved cookie for matching pattern '%s' => (%s: %s)", pattern.pattern, cookie.name, cookie.value, ) included_cookies.append(cookie) break pickle.dump(cookies, save_path.open("w+b"))
# Load all cookiesawait browser.cookies.load(file='session.dat')# Load specific cookies using patternawait browser.cookies.load( file='session.dat', pattern='(session|auth)')
# Save session after loginawait browser.cookies.save('logged_in.dat')# Later, restore sessionbrowser = await uc.start()await browser.cookies.load('logged_in.dat')tab = await browser.get('https://example.com')# Now logged in!
For more control, use the Chrome DevTools Protocol directly:
from nodriver import cdptab = await browser.get('https://example.com')# Get cookies for specific URLcookies = await tab.send( cdp.network.get_cookies(urls=['https://example.com']))# Set a cookie with all optionsawait tab.send( cdp.network.set_cookie( name='my_cookie', value='cookie_value', domain='example.com', path='/', secure=True, http_only=True, same_site=cdp.network.CookieSameSite.LAX, expires=1234567890 # Unix timestamp ))# Delete specific cookieawait tab.send( cdp.network.delete_cookies( name='my_cookie', domain='example.com' ))
from nodriver import cdp# Get all service workersworkers = await tab.send(cdp.service_worker.get_registrations())for worker in workers: print(f"Scope: {worker.scope_url}") print(f"Script: {worker.script_url}")# Unregister service workerif workers: await tab.send( cdp.service_worker.unregister( scope_url=workers[0].scope_url ) )