MCPcopy Create free account
hub / github.com/SeleniumHQ/selenium / APIRequestContext

Class APIRequestContext

py/selenium/webdriver/common/api_request_context.py:517–654  ·  view source on GitHub ↗

Makes HTTP requests with automatic browser cookie synchronization. Cookies from the browser session are sent with API requests, and cookies from API responses are synced back to the browser. Args: driver: The WebDriver instance to sync cookies with. base_url: Optional b

Source from the content-addressed store, hash-verified

515
516
517class APIRequestContext(_BaseRequestContext):
518 """Makes HTTP requests with automatic browser cookie synchronization.
519
520 Cookies from the browser session are sent with API requests, and cookies
521 from API responses are synced back to the browser.
522
523 Args:
524 driver: The WebDriver instance to sync cookies with.
525 base_url: Optional base URL prepended to relative request paths.
526 extra_headers: Optional headers included in every request.
527 timeout: Default request timeout in seconds.
528 max_redirects: Maximum number of redirects to follow.
529 fail_on_status_code: If True, raise APIRequestFailure for non-2xx responses.
530 """
531
532 def __init__(
533 self,
534 driver: "WebDriver",
535 base_url: str = "",
536 extra_headers: dict[str, str] | None = None,
537 timeout: float = 30.0,
538 max_redirects: int = 10,
539 fail_on_status_code: bool = False,
540 ) -> None:
541 super().__init__(
542 base_url=base_url,
543 extra_headers=extra_headers,
544 timeout=timeout,
545 max_redirects=max_redirects,
546 fail_on_status_code=fail_on_status_code,
547 )
548 self._driver = driver
549
550 def new_context(
551 self,
552 base_url: str = "",
553 extra_headers: dict[str, str] | None = None,
554 storage_state: dict | str | pathlib.Path | None = None,
555 fail_on_status_code: bool = False,
556 ) -> "_IsolatedAPIRequestContext":
557 """Create an isolated API request context that does not sync with the browser.
558
559 Args:
560 base_url: Optional base URL for this context.
561 extra_headers: Optional headers for this context.
562 storage_state: Optional cookies to pre-load, as a dict, JSON file path, or Path.
563 fail_on_status_code: If True, raise APIRequestFailure for non-2xx responses.
564
565 Returns:
566 An _IsolatedAPIRequestContext instance.
567 """
568 cookies: list[Cookie] = []
569 if storage_state is not None:
570 if isinstance(storage_state, (str, pathlib.Path)):
571 file_path = pathlib.Path(storage_state)
572 if not file_path.exists():
573 raise FileNotFoundError(f"Storage state file not found: {file_path}")
574 try:

Calls

no outgoing calls