# Copyright (c) Microsoft Corporation. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import pathlib import sys import typing if sys.version_info >= (3, 8): # pragma: no cover from typing import Literal else: # pragma: no cover from typing_extensions import Literal from playwright._impl._accessibility import Accessibility as AccessibilityImpl from playwright._impl._api_structures import ( Cookie, FilePayload, FloatRect, Geolocation, HttpCredentials, PdfMargins, Position, ProxySettings, ResourceTiming, SourceLocation, StorageState, ViewportSize, ) from playwright._impl._browser import Browser as BrowserImpl from playwright._impl._browser_context import BrowserContext as BrowserContextImpl from playwright._impl._browser_type import BrowserType as BrowserTypeImpl from playwright._impl._cdp_session import CDPSession as CDPSessionImpl from playwright._impl._chromium_browser_context import ( ChromiumBrowserContext as ChromiumBrowserContextImpl, ) from playwright._impl._console_message import ConsoleMessage as ConsoleMessageImpl from playwright._impl._dialog import Dialog as DialogImpl from playwright._impl._download import Download as DownloadImpl from playwright._impl._element_handle import ElementHandle as ElementHandleImpl from playwright._impl._file_chooser import FileChooser as FileChooserImpl from playwright._impl._frame import Frame as FrameImpl from playwright._impl._input import Keyboard as KeyboardImpl from playwright._impl._input import Mouse as MouseImpl from playwright._impl._input import Touchscreen as TouchscreenImpl from playwright._impl._js_handle import JSHandle as JSHandleImpl from playwright._impl._network import Request as RequestImpl from playwright._impl._network import Response as ResponseImpl from playwright._impl._network import Route as RouteImpl from playwright._impl._network import WebSocket as WebSocketImpl from playwright._impl._page import Page as PageImpl from playwright._impl._page import Worker as WorkerImpl from playwright._impl._playwright import Playwright as PlaywrightImpl from playwright._impl._selectors import Selectors as SelectorsImpl from playwright._impl._sync_base import EventContextManager, SyncBase, mapping from playwright._impl._video import Video as VideoImpl NoneType = type(None) class Request(SyncBase): def __init__(self, obj: RequestImpl): super().__init__(obj) @property def url(self) -> str: """Request.url URL of the request. Returns ------- str """ return mapping.from_maybe_impl(self._impl_obj.url) @property def resource_type(self) -> str: """Request.resource_type Contains the request's resource type as it was perceived by the rendering engine. ResourceType will be one of the following: `document`, `stylesheet`, `image`, `media`, `font`, `script`, `texttrack`, `xhr`, `fetch`, `eventsource`, `websocket`, `manifest`, `other`. > NOTE: The resource types are available as constants in [ResourceTypes]. Returns ------- str """ return mapping.from_maybe_impl(self._impl_obj.resource_type) @property def method(self) -> str: """Request.method Request's method (GET, POST, etc.) Returns ------- str """ return mapping.from_maybe_impl(self._impl_obj.method) @property def post_data(self) -> typing.Optional[str]: """Request.post_data Request's post body, if any. Returns ------- Union[str, NoneType] """ return mapping.from_maybe_impl(self._impl_obj.post_data) @property def post_data_json(self) -> typing.Optional[typing.Any]: """Request.post_data_json Returns parsed request's body for `form-urlencoded` and JSON as a fallback if any. When the response is `application/x-www-form-urlencoded` then a key/value object of the values will be returned. Otherwise it will be parsed as JSON. Returns ------- Union[Any, NoneType] """ return mapping.from_maybe_impl(self._impl_obj.post_data_json) @property def post_data_buffer(self) -> typing.Optional[bytes]: """Request.post_data_buffer Request's post body in a binary form, if any. Returns ------- Union[bytes, NoneType] """ return mapping.from_maybe_impl(self._impl_obj.post_data_buffer) @property def headers(self) -> typing.Dict[str, str]: """Request.headers An object with HTTP headers associated with the request. All header names are lower-case. Returns ------- Dict[str, str] """ return mapping.from_maybe_impl(self._impl_obj.headers) @property def frame(self) -> "Frame": """Request.frame Returns the `Frame` that initiated this request. Returns ------- Frame """ return mapping.from_impl(self._impl_obj.frame) @property def redirected_from(self) -> typing.Optional["Request"]: """Request.redirected_from Request that was redirected by the server to this one, if any. When the server responds with a redirect, Playwright creates a new `Request` object. The two requests are connected by `redirectedFrom()` and `redirectedTo()` methods. When multiple server redirects has happened, it is possible to construct the whole redirect chain by repeatedly calling `redirectedFrom()`. For example, if the website `http://example.com` redirects to `https://example.com`: ```py response = page.goto(\"http://example.com\") print(response.request.redirected_from.url) # \"http://example.com\" ``` If the website `https://google.com` has no redirects: ```py response = page.goto(\"https://google.com\") print(response.request.redirected_from) # None ``` Returns ------- Union[Request, NoneType] """ return mapping.from_impl_nullable(self._impl_obj.redirected_from) @property def redirected_to(self) -> typing.Optional["Request"]: """Request.redirected_to New request issued by the browser if the server responded with redirect. This method is the opposite of `request.redirected_from()`: ```py assert request.redirected_from.redirected_to == request ``` Returns ------- Union[Request, NoneType] """ return mapping.from_impl_nullable(self._impl_obj.redirected_to) @property def failure(self) -> typing.Optional[str]: """Request.failure The method returns `null` unless this request has failed, as reported by `requestfailed` event. Example of logging of all the failed requests: ```py page.on(\"requestfailed\", lambda request: print(request.url + \" \" + request.failure)) ``` Returns ------- Union[str, NoneType] """ return mapping.from_maybe_impl(self._impl_obj.failure) @property def timing(self) -> ResourceTiming: """Request.timing Returns resource timing information for given request. Most of the timing values become available upon the response, `responseEnd` becomes available when request finishes. Find more information at [Resource Timing API](https://developer.mozilla.org/en-US/docs/Web/API/PerformanceResourceTiming). ```py with page.expect_event(\"requestfinished\") as request_info: page.goto(\"http://example.com\") request = request_info.value print(request.timing) ``` Returns ------- {startTime: float, domainLookupStart: float, domainLookupEnd: float, connectStart: float, secureConnectionStart: float, connectEnd: float, requestStart: float, responseStart: float, responseEnd: float} """ return mapping.from_impl(self._impl_obj.timing) def response(self) -> typing.Optional["Response"]: """Request.response Returns the matching `Response` object, or `null` if the response was not received due to error. Returns ------- Union[Response, NoneType] """ return mapping.from_impl_nullable( self._sync("request.response", self._impl_obj.response()) ) def is_navigation_request(self) -> bool: """Request.is_navigation_request Whether this request is driving frame's navigation. Returns ------- bool """ return mapping.from_maybe_impl(self._impl_obj.is_navigation_request()) mapping.register(RequestImpl, Request) class Response(SyncBase): def __init__(self, obj: ResponseImpl): super().__init__(obj) @property def url(self) -> str: """Response.url Contains the URL of the response. Returns ------- str """ return mapping.from_maybe_impl(self._impl_obj.url) @property def ok(self) -> bool: """Response.ok Contains a boolean stating whether the response was successful (status in the range 200-299) or not. Returns ------- bool """ return mapping.from_maybe_impl(self._impl_obj.ok) @property def status(self) -> int: """Response.status Contains the status code of the response (e.g., 200 for a success). Returns ------- int """ return mapping.from_maybe_impl(self._impl_obj.status) @property def status_text(self) -> str: """Response.status_text Contains the status text of the response (e.g. usually an \"OK\" for a success). Returns ------- str """ return mapping.from_maybe_impl(self._impl_obj.status_text) @property def headers(self) -> typing.Dict[str, str]: """Response.headers Returns the object with HTTP headers associated with the response. All header names are lower-case. Returns ------- Dict[str, str] """ return mapping.from_maybe_impl(self._impl_obj.headers) @property def request(self) -> "Request": """Response.request Returns the matching `Request` object. Returns ------- Request """ return mapping.from_impl(self._impl_obj.request) @property def frame(self) -> "Frame": """Response.frame Returns the `Frame` that initiated this response. Returns ------- Frame """ return mapping.from_impl(self._impl_obj.frame) def finished(self) -> typing.Optional[str]: """Response.finished Waits for this response to finish, returns failure error if request failed. Returns ------- Union[str, NoneType] """ return mapping.from_maybe_impl( self._sync("response.finished", self._impl_obj.finished()) ) def body(self) -> bytes: """Response.body Returns the buffer with response body. Returns ------- bytes """ return mapping.from_maybe_impl( self._sync("response.body", self._impl_obj.body()) ) def text(self) -> str: """Response.text Returns the text representation of response body. Returns ------- str """ return mapping.from_maybe_impl( self._sync("response.text", self._impl_obj.text()) ) def json(self) -> typing.Any: """Response.json Returns the JSON representation of response body. This method will throw if the response body is not parsable via `JSON.parse`. Returns ------- Any """ return mapping.from_maybe_impl( self._sync("response.json", self._impl_obj.json()) ) mapping.register(ResponseImpl, Response) class Route(SyncBase): def __init__(self, obj: RouteImpl): super().__init__(obj) @property def request(self) -> "Request": """Route.request A request to be routed. Returns ------- Request """ return mapping.from_impl(self._impl_obj.request) def abort(self, error_code: str = None) -> NoneType: """Route.abort Aborts the route's request. Parameters ---------- error_code : Union[str, NoneType] Optional error code. Defaults to `failed`, could be one of the following: - `'aborted'` - An operation was aborted (due to user action) - `'accessdenied'` - Permission to access a resource, other than the network, was denied - `'addressunreachable'` - The IP address is unreachable. This usually means that there is no route to the specified host or network. - `'blockedbyclient'` - The client chose to block the request. - `'blockedbyresponse'` - The request failed because the response was delivered along with requirements which are not met ('X-Frame-Options' and 'Content-Security-Policy' ancestor checks, for instance). - `'connectionaborted'` - A connection timed out as a result of not receiving an ACK for data sent. - `'connectionclosed'` - A connection was closed (corresponding to a TCP FIN). - `'connectionfailed'` - A connection attempt failed. - `'connectionrefused'` - A connection attempt was refused. - `'connectionreset'` - A connection was reset (corresponding to a TCP RST). - `'internetdisconnected'` - The Internet connection has been lost. - `'namenotresolved'` - The host name could not be resolved. - `'timedout'` - An operation timed out. - `'failed'` - A generic failure occurred. """ return mapping.from_maybe_impl( self._sync("route.abort", self._impl_obj.abort(errorCode=error_code)) ) def fulfill( self, *, status: int = None, headers: typing.Optional[typing.Dict[str, str]] = None, body: typing.Union[str, bytes] = None, path: typing.Union[str, pathlib.Path] = None, content_type: str = None ) -> NoneType: """Route.fulfill Fulfills route's request with given response. An example of fulfilling all requests with 404 responses: ```py page.route(\"**/*\", lambda route: route.fulfill( status=404, content_type=\"text/plain\", body=\"not found!\")) ``` An example of serving static file: ```py page.route(\"**/xhr_endpoint\", lambda route: route.fulfill(path=\"mock_data.json\")) ``` Parameters ---------- status : Union[int, NoneType] Response status code, defaults to `200`. headers : Union[Dict[str, str], NoneType] Response headers. Header values will be converted to a string. body : Union[bytes, str, NoneType] Response body. path : Union[pathlib.Path, str, NoneType] File path to respond with. The content type will be inferred from file extension. If `path` is a relative path, then it is resolved relative to the current working directory. content_type : Union[str, NoneType] If set, equals to setting `Content-Type` response header. """ return mapping.from_maybe_impl( self._sync( "route.fulfill", self._impl_obj.fulfill( status=status, headers=mapping.to_impl(headers), body=body, path=path, contentType=content_type, ), ) ) def continue_( self, *, url: str = None, method: str = None, headers: typing.Optional[typing.Dict[str, str]] = None, post_data: typing.Union[str, bytes] = None ) -> NoneType: """Route.continue_ Continues route's request with optional overrides. ```py def handle(route, request): # override headers headers = { **request.headers, \"foo\": \"bar\" # set \"foo\" header \"origin\": None # remove \"origin\" header } route.continue(headers=headers) } page.route(\"**/*\", handle) ``` Parameters ---------- url : Union[str, NoneType] If set changes the request URL. New URL must have same protocol as original one. method : Union[str, NoneType] If set changes the request method (e.g. GET or POST) headers : Union[Dict[str, str], NoneType] If set changes the request HTTP headers. Header values will be converted to a string. post_data : Union[bytes, str, NoneType] If set changes the post data of request """ return mapping.from_maybe_impl( self._sync( "route.continue_", self._impl_obj.continue_( url=url, method=method, headers=mapping.to_impl(headers), postData=post_data, ), ) ) mapping.register(RouteImpl, Route) class WebSocket(SyncBase): def __init__(self, obj: WebSocketImpl): super().__init__(obj) @property def url(self) -> str: """WebSocket.url Contains the URL of the WebSocket. Returns ------- str """ return mapping.from_maybe_impl(self._impl_obj.url) def expect_event( self, event: str, predicate: typing.Callable = None, *, timeout: float = None ) -> EventContextManager: """WebSocket.expect_event Waits for event to fire and passes its value into the predicate function. Returns when the predicate returns truthy value. Will throw an error if the webSocket is closed before the event is fired. Returns the event data value. Parameters ---------- event : str Event name, same one would pass into `webSocket.on(event)`. predicate : Union[Callable, NoneType] Receives the event data and resolves to truthy value when the waiting should resolve. timeout : Union[float, NoneType] Maximum time to wait for in milliseconds. Defaults to `30000` (30 seconds). Pass `0` to disable timeout. The default value can be changed by using the `browser_context.set_default_timeout()`. Returns ------- EventContextManager """ return EventContextManager( self, self._impl_obj.expect_event( event=event, predicate=self._wrap_handler(predicate), timeout=timeout ).future, ) def wait_for_event( self, event: str, predicate: typing.Callable = None, *, timeout: float = None ) -> typing.Any: """WebSocket.wait_for_event > NOTE: In most cases, you should use `web_socket.expect_event()`. Waits for given `event` to fire. If predicate is provided, it passes event's value into the `predicate` function and waits for `predicate(event)` to return a truthy value. Will throw an error if the socket is closed before the `event` is fired. Parameters ---------- event : str Event name, same one typically passed into `*.on(event)`. predicate : Union[Callable, NoneType] Receives the event data and resolves to truthy value when the waiting should resolve. timeout : Union[float, NoneType] Maximum time to wait for in milliseconds. Defaults to `30000` (30 seconds). Pass `0` to disable timeout. The default value can be changed by using the `browser_context.set_default_timeout()`. Returns ------- Any """ return mapping.from_maybe_impl( self._sync( "web_socket.wait_for_event", self._impl_obj.wait_for_event( event=event, predicate=self._wrap_handler(predicate), timeout=timeout, ), ) ) def is_closed(self) -> bool: """WebSocket.is_closed Indicates that the web socket has been closed. Returns ------- bool """ return mapping.from_maybe_impl(self._impl_obj.is_closed()) mapping.register(WebSocketImpl, WebSocket) class Keyboard(SyncBase): def __init__(self, obj: KeyboardImpl): super().__init__(obj) def down(self, key: str) -> NoneType: """Keyboard.down Dispatches a `keydown` event. `key` can specify the intended [keyboardEvent.key](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key) value or a single character to generate the text for. A superset of the `key` values can be found [here](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values). Examples of the keys are: `F1` - `F12`, `Digit0`- `Digit9`, `KeyA`- `KeyZ`, `Backquote`, `Minus`, `Equal`, `Backslash`, `Backspace`, `Tab`, `Delete`, `Escape`, `ArrowDown`, `End`, `Enter`, `Home`, `Insert`, `PageDown`, `PageUp`, `ArrowRight`, `ArrowUp`, etc. Following modification shortcuts are also supported: `Shift`, `Control`, `Alt`, `Meta`, `ShiftLeft`. Holding down `Shift` will type the text that corresponds to the `key` in the upper case. If `key` is a single character, it is case-sensitive, so the values `a` and `A` will generate different respective texts. If `key` is a modifier key, `Shift`, `Meta`, `Control`, or `Alt`, subsequent key presses will be sent with that modifier active. To release the modifier key, use `keyboard.up()`. After the key is pressed once, subsequent calls to `keyboard.down()` will have [repeat](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/repeat) set to true. To release the key, use `keyboard.up()`. > NOTE: Modifier keys DO influence `keyboard.down`. Holding down `Shift` will type the text in upper case. Parameters ---------- key : str Name of the key to press or a character to generate, such as `ArrowLeft` or `a`. """ return mapping.from_maybe_impl( self._sync("keyboard.down", self._impl_obj.down(key=key)) ) def up(self, key: str) -> NoneType: """Keyboard.up Dispatches a `keyup` event. Parameters ---------- key : str Name of the key to press or a character to generate, such as `ArrowLeft` or `a`. """ return mapping.from_maybe_impl( self._sync("keyboard.up", self._impl_obj.up(key=key)) ) def insert_text(self, text: str) -> NoneType: """Keyboard.insert_text Dispatches only `input` event, does not emit the `keydown`, `keyup` or `keypress` events. ```py page.keyboard.insert_text(\"嗨\") ``` > NOTE: Modifier keys DO NOT effect `keyboard.insertText`. Holding down `Shift` will not type the text in upper case. Parameters ---------- text : str Sets input to the specified text value. """ return mapping.from_maybe_impl( self._sync("keyboard.insert_text", self._impl_obj.insert_text(text=text)) ) def type(self, text: str, *, delay: float = None) -> NoneType: """Keyboard.type Sends a `keydown`, `keypress`/`input`, and `keyup` event for each character in the text. To press a special key, like `Control` or `ArrowDown`, use `keyboard.press()`. ```py page.keyboard.type(\"Hello\") # types instantly page.keyboard.type(\"World\", delay=100) # types slower, like a user ``` > NOTE: Modifier keys DO NOT effect `keyboard.type`. Holding down `Shift` will not type the text in upper case. Parameters ---------- text : str A text to type into a focused element. delay : Union[float, NoneType] Time to wait between key presses in milliseconds. Defaults to 0. """ return mapping.from_maybe_impl( self._sync("keyboard.type", self._impl_obj.type(text=text, delay=delay)) ) def press(self, key: str, *, delay: float = None) -> NoneType: """Keyboard.press `key` can specify the intended [keyboardEvent.key](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key) value or a single character to generate the text for. A superset of the `key` values can be found [here](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values). Examples of the keys are: `F1` - `F12`, `Digit0`- `Digit9`, `KeyA`- `KeyZ`, `Backquote`, `Minus`, `Equal`, `Backslash`, `Backspace`, `Tab`, `Delete`, `Escape`, `ArrowDown`, `End`, `Enter`, `Home`, `Insert`, `PageDown`, `PageUp`, `ArrowRight`, `ArrowUp`, etc. Following modification shortcuts are also supported: `Shift`, `Control`, `Alt`, `Meta`, `ShiftLeft`. Holding down `Shift` will type the text that corresponds to the `key` in the upper case. If `key` is a single character, it is case-sensitive, so the values `a` and `A` will generate different respective texts. Shortcuts such as `key: \"Control+o\"` or `key: \"Control+Shift+T\"` are supported as well. When speficied with the modifier, modifier is pressed and being held while the subsequent key is being pressed. ```py page = browser.new_page() page.goto(\"https://keycode.info\") page.keyboard.press(\"a\") page.screenshot(path=\"a.png\") page.keyboard.press(\"ArrowLeft\") page.screenshot(path=\"arrow_left.png\") page.keyboard.press(\"Shift+O\") page.screenshot(path=\"o.png\") browser.close() ``` Shortcut for `keyboard.down()` and `keyboard.up()`. Parameters ---------- key : str Name of the key to press or a character to generate, such as `ArrowLeft` or `a`. delay : Union[float, NoneType] Time to wait between `keydown` and `keyup` in milliseconds. Defaults to 0. """ return mapping.from_maybe_impl( self._sync("keyboard.press", self._impl_obj.press(key=key, delay=delay)) ) mapping.register(KeyboardImpl, Keyboard) class Mouse(SyncBase): def __init__(self, obj: MouseImpl): super().__init__(obj) def move(self, x: float, y: float, *, steps: int = None) -> NoneType: """Mouse.move Dispatches a `mousemove` event. Parameters ---------- x : float y : float steps : Union[int, NoneType] defaults to 1. Sends intermediate `mousemove` events. """ return mapping.from_maybe_impl( self._sync("mouse.move", self._impl_obj.move(x=x, y=y, steps=steps)) ) def down( self, *, button: Literal["left", "middle", "right"] = None, click_count: int = None ) -> NoneType: """Mouse.down Dispatches a `mousedown` event. Parameters ---------- button : Union["left", "middle", "right", NoneType] Defaults to `left`. click_count : Union[int, NoneType] defaults to 1. See [UIEvent.detail]. """ return mapping.from_maybe_impl( self._sync( "mouse.down", self._impl_obj.down(button=button, clickCount=click_count) ) ) def up( self, *, button: Literal["left", "middle", "right"] = None, click_count: int = None ) -> NoneType: """Mouse.up Dispatches a `mouseup` event. Parameters ---------- button : Union["left", "middle", "right", NoneType] Defaults to `left`. click_count : Union[int, NoneType] defaults to 1. See [UIEvent.detail]. """ return mapping.from_maybe_impl( self._sync( "mouse.up", self._impl_obj.up(button=button, clickCount=click_count) ) ) def click( self, x: float, y: float, *, delay: float = None, button: Literal["left", "middle", "right"] = None, click_count: int = None ) -> NoneType: """Mouse.click Shortcut for `mouse.move()`, `mouse.down()`, `mouse.up()`. Parameters ---------- x : float y : float delay : Union[float, NoneType] Time to wait between `mousedown` and `mouseup` in milliseconds. Defaults to 0. button : Union["left", "middle", "right", NoneType] Defaults to `left`. click_count : Union[int, NoneType] defaults to 1. See [UIEvent.detail]. """ return mapping.from_maybe_impl( self._sync( "mouse.click", self._impl_obj.click( x=x, y=y, delay=delay, button=button, clickCount=click_count ), ) ) def dblclick( self, x: float, y: float, *, delay: float = None, button: Literal["left", "middle", "right"] = None ) -> NoneType: """Mouse.dblclick Shortcut for `mouse.move()`, `mouse.down()`, `mouse.up()`, `mouse.down()` and `mouse.up()`. Parameters ---------- x : float y : float delay : Union[float, NoneType] Time to wait between `mousedown` and `mouseup` in milliseconds. Defaults to 0. button : Union["left", "middle", "right", NoneType] Defaults to `left`. """ return mapping.from_maybe_impl( self._sync( "mouse.dblclick", self._impl_obj.dblclick(x=x, y=y, delay=delay, button=button), ) ) mapping.register(MouseImpl, Mouse) class Touchscreen(SyncBase): def __init__(self, obj: TouchscreenImpl): super().__init__(obj) def tap(self, x: float, y: float) -> NoneType: """Touchscreen.tap Dispatches a `touchstart` and `touchend` event with a single touch at the position (`x`,`y`). Parameters ---------- x : float y : float """ return mapping.from_maybe_impl( self._sync("touchscreen.tap", self._impl_obj.tap(x=x, y=y)) ) mapping.register(TouchscreenImpl, Touchscreen) class JSHandle(SyncBase): def __init__(self, obj: JSHandleImpl): super().__init__(obj) def evaluate(self, expression: str, arg: typing.Any = None) -> typing.Any: """JSHandle.evaluate Returns the return value of `expression`. This method passes this handle as the first argument to `expression`. If `expression` returns a [Promise], then `handle.evaluate` would wait for the promise to resolve and return its value. Examples: ```py tweet_handle = page.query_selector(\".tweet .retweets\") assert tweet_handle.evaluate(\"node => node.innerText\") == \"10 retweets\" ``` Parameters ---------- expression : str JavaScript expression to be evaluated in the browser context. If it looks like a function declaration, it is interpreted as a function. Otherwise, evaluated as an expression. arg : Union[Any, NoneType] Optional argument to pass to `expression`. Returns ------- Any """ return mapping.from_maybe_impl( self._sync( "js_handle.evaluate", self._impl_obj.evaluate( expression=expression, arg=mapping.to_impl(arg) ), ) ) def evaluate_handle(self, expression: str, arg: typing.Any = None) -> "JSHandle": """JSHandle.evaluate_handle Returns the return value of `expression` as a `JSHandle`. This method passes this handle as the first argument to `expression`. The only difference between `jsHandle.evaluate` and `jsHandle.evaluateHandle` is that `jsHandle.evaluateHandle` returns `JSHandle`. If the function passed to the `jsHandle.evaluateHandle` returns a [Promise], then `jsHandle.evaluateHandle` would wait for the promise to resolve and return its value. See `page.evaluate_handle()` for more details. Parameters ---------- expression : str JavaScript expression to be evaluated in the browser context. If it looks like a function declaration, it is interpreted as a function. Otherwise, evaluated as an expression. arg : Union[Any, NoneType] Optional argument to pass to `expression`. Returns ------- JSHandle """ return mapping.from_impl( self._sync( "js_handle.evaluate_handle", self._impl_obj.evaluate_handle( expression=expression, arg=mapping.to_impl(arg) ), ) ) def get_property(self, property_name: str) -> "JSHandle": """JSHandle.get_property Fetches a single property from the referenced object. Parameters ---------- property_name : str property to get Returns ------- JSHandle """ return mapping.from_impl( self._sync( "js_handle.get_property", self._impl_obj.get_property(propertyName=property_name), ) ) def get_properties(self) -> typing.Dict[str, "JSHandle"]: """JSHandle.get_properties The method returns a map with **own property names** as keys and JSHandle instances for the property values. ```py handle = page.evaluate_handle(\"{window, document}\") properties = handle.get_properties() window_handle = properties.get(\"window\") document_handle = properties.get(\"document\") handle.dispose() ``` Returns ------- Dict[str, JSHandle] """ return mapping.from_impl_dict( self._sync("js_handle.get_properties", self._impl_obj.get_properties()) ) def as_element(self) -> typing.Optional["ElementHandle"]: """JSHandle.as_element Returns either `null` or the object handle itself, if the object handle is an instance of `ElementHandle`. Returns ------- Union[ElementHandle, NoneType] """ return mapping.from_impl_nullable(self._impl_obj.as_element()) def dispose(self) -> NoneType: """JSHandle.dispose The `jsHandle.dispose` method stops referencing the element handle. """ return mapping.from_maybe_impl( self._sync("js_handle.dispose", self._impl_obj.dispose()) ) def json_value(self) -> typing.Any: """JSHandle.json_value Returns a JSON representation of the object. If the object has a `toJSON` function, it **will not be called**. > NOTE: The method will return an empty JSON object if the referenced object is not stringifiable. It will throw an error if the object has circular references. Returns ------- Any """ return mapping.from_maybe_impl( self._sync("js_handle.json_value", self._impl_obj.json_value()) ) mapping.register(JSHandleImpl, JSHandle) class ElementHandle(JSHandle): def __init__(self, obj: ElementHandleImpl): super().__init__(obj) def as_element(self) -> typing.Optional["ElementHandle"]: """ElementHandle.as_element Returns either `null` or the object handle itself, if the object handle is an instance of `ElementHandle`. Returns ------- Union[ElementHandle, NoneType] """ return mapping.from_impl_nullable(self._impl_obj.as_element()) def owner_frame(self) -> typing.Optional["Frame"]: """ElementHandle.owner_frame Returns the frame containing the given element. Returns ------- Union[Frame, NoneType] """ return mapping.from_impl_nullable( self._sync("element_handle.owner_frame", self._impl_obj.owner_frame()) ) def content_frame(self) -> typing.Optional["Frame"]: """ElementHandle.content_frame Returns the content frame for element handles referencing iframe nodes, or `null` otherwise Returns ------- Union[Frame, NoneType] """ return mapping.from_impl_nullable( self._sync("element_handle.content_frame", self._impl_obj.content_frame()) ) def get_attribute(self, name: str) -> typing.Optional[str]: """ElementHandle.get_attribute Returns element attribute value. Parameters ---------- name : str Attribute name to get the value for. Returns ------- Union[str, NoneType] """ return mapping.from_maybe_impl( self._sync( "element_handle.get_attribute", self._impl_obj.get_attribute(name=name) ) ) def text_content(self) -> typing.Optional[str]: """ElementHandle.text_content Returns the `node.textContent`. Returns ------- Union[str, NoneType] """ return mapping.from_maybe_impl( self._sync("element_handle.text_content", self._impl_obj.text_content()) ) def inner_text(self) -> str: """ElementHandle.inner_text Returns the `element.innerText`. Returns ------- str """ return mapping.from_maybe_impl( self._sync("element_handle.inner_text", self._impl_obj.inner_text()) ) def inner_html(self) -> str: """ElementHandle.inner_html Returns the `element.innerHTML`. Returns ------- str """ return mapping.from_maybe_impl( self._sync("element_handle.inner_html", self._impl_obj.inner_html()) ) def is_checked(self) -> bool: """ElementHandle.is_checked Returns whether the element is checked. Throws if the element is not a checkbox or radio input. Returns ------- bool """ return mapping.from_maybe_impl( self._sync("element_handle.is_checked", self._impl_obj.is_checked()) ) def is_disabled(self) -> bool: """ElementHandle.is_disabled Returns whether the element is disabled, the opposite of [enabled](./actionability.md#enabled). Returns ------- bool """ return mapping.from_maybe_impl( self._sync("element_handle.is_disabled", self._impl_obj.is_disabled()) ) def is_editable(self) -> bool: """ElementHandle.is_editable Returns whether the element is [editable](./actionability.md#editable). Returns ------- bool """ return mapping.from_maybe_impl( self._sync("element_handle.is_editable", self._impl_obj.is_editable()) ) def is_enabled(self) -> bool: """ElementHandle.is_enabled Returns whether the element is [enabled](./actionability.md#enabled). Returns ------- bool """ return mapping.from_maybe_impl( self._sync("element_handle.is_enabled", self._impl_obj.is_enabled()) ) def is_hidden(self) -> bool: """ElementHandle.is_hidden Returns whether the element is hidden, the opposite of [visible](./actionability.md#visible). Returns ------- bool """ return mapping.from_maybe_impl( self._sync("element_handle.is_hidden", self._impl_obj.is_hidden()) ) def is_visible(self) -> bool: """ElementHandle.is_visible Returns whether the element is [visible](./actionability.md#visible). Returns ------- bool """ return mapping.from_maybe_impl( self._sync("element_handle.is_visible", self._impl_obj.is_visible()) ) def dispatch_event(self, type: str, event_init: typing.Dict = None) -> NoneType: """ElementHandle.dispatch_event The snippet below dispatches the `click` event on the element. Regardless of the visibility state of the elment, `click` is dispatched. This is equivalend to calling [element.click()](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/click). ```py element_handle.dispatch_event(\"click\") ``` Under the hood, it creates an instance of an event based on the given `type`, initializes it with `eventInit` properties and dispatches it on the element. Events are `composed`, `cancelable` and bubble by default. Since `eventInit` is event-specific, please refer to the events documentation for the lists of initial properties: - [DragEvent](https://developer.mozilla.org/en-US/docs/Web/API/DragEvent/DragEvent) - [FocusEvent](https://developer.mozilla.org/en-US/docs/Web/API/FocusEvent/FocusEvent) - [KeyboardEvent](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/KeyboardEvent) - [MouseEvent](https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/MouseEvent) - [PointerEvent](https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent/PointerEvent) - [TouchEvent](https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent/TouchEvent) - [Event](https://developer.mozilla.org/en-US/docs/Web/API/Event/Event) You can also specify `JSHandle` as the property value if you want live objects to be passed into the event: ```py # note you can only create data_transfer in chromium and firefox data_transfer = page.evaluate_handle(\"new DataTransfer()\") element_handle.dispatch_event(\"#source\", \"dragstart\", {\"dataTransfer\": data_transfer}) ``` Parameters ---------- type : str DOM event type: `"click"`, `"dragstart"`, etc. event_init : Union[Dict, NoneType] Optional event-specific initialization properties. """ return mapping.from_maybe_impl( self._sync( "element_handle.dispatch_event", self._impl_obj.dispatch_event( type=type, eventInit=mapping.to_impl(event_init) ), ) ) def scroll_into_view_if_needed(self, *, timeout: float = None) -> NoneType: """ElementHandle.scroll_into_view_if_needed This method waits for [actionability](./actionability.md) checks, then tries to scroll element into view, unless it is completely visible as defined by [IntersectionObserver](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API)'s `ratio`. Throws when `elementHandle` does not point to an element [connected](https://developer.mozilla.org/en-US/docs/Web/API/Node/isConnected) to a Document or a ShadowRoot. Parameters ---------- timeout : Union[float, NoneType] Maximum time in milliseconds, defaults to 30 seconds, pass `0` to disable timeout. The default value can be changed by using the `browser_context.set_default_timeout()` or `page.set_default_timeout()` methods. """ return mapping.from_maybe_impl( self._sync( "element_handle.scroll_into_view_if_needed", self._impl_obj.scroll_into_view_if_needed(timeout=timeout), ) ) def hover( self, *, modifiers: typing.Optional[ typing.List[Literal["Alt", "Control", "Meta", "Shift"]] ] = None, position: Position = None, timeout: float = None, force: bool = None ) -> NoneType: """ElementHandle.hover This method hovers over the element by performing the following steps: 1. Wait for [actionability](./actionability.md) checks on the element, unless `force` option is set. 1. Scroll the element into view if needed. 1. Use `page.mouse` to hover over the center of the element, or the specified `position`. 1. Wait for initiated navigations to either succeed or fail, unless `noWaitAfter` option is set. If the element is detached from the DOM at any moment during the action, this method rejects. When all steps combined have not finished during the specified `timeout`, this method rejects with a `TimeoutError`. Passing zero timeout disables this. Parameters ---------- modifiers : Union[List[Union["Alt", "Control", "Meta", "Shift"]], NoneType] Modifier keys to press. Ensures that only these modifiers are pressed during the operation, and then restores current modifiers back. If not specified, currently pressed modifiers are used. position : Union[{x: float, y: float}, NoneType] A point to use relative to the top-left corner of element padding box. If not specified, uses some visible point of the element. timeout : Union[float, NoneType] Maximum time in milliseconds, defaults to 30 seconds, pass `0` to disable timeout. The default value can be changed by using the `browser_context.set_default_timeout()` or `page.set_default_timeout()` methods. force : Union[bool, NoneType] Whether to bypass the [actionability](./actionability.md) checks. Defaults to `false`. """ return mapping.from_maybe_impl( self._sync( "element_handle.hover", self._impl_obj.hover( modifiers=modifiers, position=position, timeout=timeout, force=force ), ) ) def click( self, *, modifiers: typing.Optional[ typing.List[Literal["Alt", "Control", "Meta", "Shift"]] ] = None, position: Position = None, delay: float = None, button: Literal["left", "middle", "right"] = None, click_count: int = None, timeout: float = None, force: bool = None, no_wait_after: bool = None ) -> NoneType: """ElementHandle.click This method clicks the element by performing the following steps: 1. Wait for [actionability](./actionability.md) checks on the element, unless `force` option is set. 1. Scroll the element into view if needed. 1. Use `page.mouse` to click in the center of the element, or the specified `position`. 1. Wait for initiated navigations to either succeed or fail, unless `noWaitAfter` option is set. If the element is detached from the DOM at any moment during the action, this method rejects. When all steps combined have not finished during the specified `timeout`, this method rejects with a `TimeoutError`. Passing zero timeout disables this. Parameters ---------- modifiers : Union[List[Union["Alt", "Control", "Meta", "Shift"]], NoneType] Modifier keys to press. Ensures that only these modifiers are pressed during the operation, and then restores current modifiers back. If not specified, currently pressed modifiers are used. position : Union[{x: float, y: float}, NoneType] A point to use relative to the top-left corner of element padding box. If not specified, uses some visible point of the element. delay : Union[float, NoneType] Time to wait between `mousedown` and `mouseup` in milliseconds. Defaults to 0. button : Union["left", "middle", "right", NoneType] Defaults to `left`. click_count : Union[int, NoneType] defaults to 1. See [UIEvent.detail]. timeout : Union[float, NoneType] Maximum time in milliseconds, defaults to 30 seconds, pass `0` to disable timeout. The default value can be changed by using the `browser_context.set_default_timeout()` or `page.set_default_timeout()` methods. force : Union[bool, NoneType] Whether to bypass the [actionability](./actionability.md) checks. Defaults to `false`. no_wait_after : Union[bool, NoneType] Actions that initiate navigations are waiting for these navigations to happen and for pages to start loading. You can opt out of waiting via setting this flag. You would only need this option in the exceptional cases such as navigating to inaccessible pages. Defaults to `false`. """ return mapping.from_maybe_impl( self._sync( "element_handle.click", self._impl_obj.click( modifiers=modifiers, position=position, delay=delay, button=button, clickCount=click_count, timeout=timeout, force=force, noWaitAfter=no_wait_after, ), ) ) def dblclick( self, *, modifiers: typing.Optional[ typing.List[Literal["Alt", "Control", "Meta", "Shift"]] ] = None, position: Position = None, delay: float = None, button: Literal["left", "middle", "right"] = None, timeout: float = None, force: bool = None, no_wait_after: bool = None ) -> NoneType: """ElementHandle.dblclick This method double clicks the element by performing the following steps: 1. Wait for [actionability](./actionability.md) checks on the element, unless `force` option is set. 1. Scroll the element into view if needed. 1. Use `page.mouse` to double click in the center of the element, or the specified `position`. 1. Wait for initiated navigations to either succeed or fail, unless `noWaitAfter` option is set. Note that if the first click of the `dblclick()` triggers a navigation event, this method will reject. If the element is detached from the DOM at any moment during the action, this method rejects. When all steps combined have not finished during the specified `timeout`, this method rejects with a `TimeoutError`. Passing zero timeout disables this. > NOTE: `elementHandle.dblclick()` dispatches two `click` events and a single `dblclick` event. Parameters ---------- modifiers : Union[List[Union["Alt", "Control", "Meta", "Shift"]], NoneType] Modifier keys to press. Ensures that only these modifiers are pressed during the operation, and then restores current modifiers back. If not specified, currently pressed modifiers are used. position : Union[{x: float, y: float}, NoneType] A point to use relative to the top-left corner of element padding box. If not specified, uses some visible point of the element. delay : Union[float, NoneType] Time to wait between `mousedown` and `mouseup` in milliseconds. Defaults to 0. button : Union["left", "middle", "right", NoneType] Defaults to `left`. timeout : Union[float, NoneType] Maximum time in milliseconds, defaults to 30 seconds, pass `0` to disable timeout. The default value can be changed by using the `browser_context.set_default_timeout()` or `page.set_default_timeout()` methods. force : Union[bool, NoneType] Whether to bypass the [actionability](./actionability.md) checks. Defaults to `false`. no_wait_after : Union[bool, NoneType] Actions that initiate navigations are waiting for these navigations to happen and for pages to start loading. You can opt out of waiting via setting this flag. You would only need this option in the exceptional cases such as navigating to inaccessible pages. Defaults to `false`. """ return mapping.from_maybe_impl( self._sync( "element_handle.dblclick", self._impl_obj.dblclick( modifiers=modifiers, position=position, delay=delay, button=button, timeout=timeout, force=force, noWaitAfter=no_wait_after, ), ) ) def select_option( self, value: typing.Union[str, typing.List[str]] = None, *, index: typing.Union[int, typing.List[int]] = None, label: typing.Union[str, typing.List[str]] = None, element: typing.Union["ElementHandle", typing.List["ElementHandle"]] = None, timeout: float = None, no_wait_after: bool = None ) -> typing.List[str]: """ElementHandle.select_option Returns the array of option values that have been successfully selected. Triggers a `change` and `input` event once all the provided options have been selected. If element is not a `` element. ```py # single selection matching the value handle.select_option(\"blue\") # single selection matching both the label handle.select_option(label=\"blue\") # multiple selection handle.select_option(value=[\"red\", \"green\", \"blue\"]) ``` ```py # single selection matching the value handle.select_option(\"blue\") # single selection matching both the value and the label handle.select_option(label=\"blue\") # multiple selection handle.select_option(\"red\", \"green\", \"blue\") # multiple selection for blue, red and second option handle.select_option(value=\"blue\", { index: 2 }, \"red\") ``` Parameters ---------- value : Union[List[str], str, NoneType] Options to select by value. If the `` has the `multiple` attribute, all given options are selected, otherwise only the first option matching one of the passed options is selected. Optional. element : Union[ElementHandle, List[ElementHandle], NoneType] Option elements to select. Optional. timeout : Union[float, NoneType] Maximum time in milliseconds, defaults to 30 seconds, pass `0` to disable timeout. The default value can be changed by using the `browser_context.set_default_timeout()` or `page.set_default_timeout()` methods. no_wait_after : Union[bool, NoneType] Actions that initiate navigations are waiting for these navigations to happen and for pages to start loading. You can opt out of waiting via setting this flag. You would only need this option in the exceptional cases such as navigating to inaccessible pages. Defaults to `false`. Returns ------- List[str] """ return mapping.from_maybe_impl( self._sync( "element_handle.select_option", self._impl_obj.select_option( value=value, index=index, label=label, element=mapping.to_impl(element), timeout=timeout, noWaitAfter=no_wait_after, ), ) ) def tap( self, *, modifiers: typing.Optional[ typing.List[Literal["Alt", "Control", "Meta", "Shift"]] ] = None, position: Position = None, timeout: float = None, force: bool = None, no_wait_after: bool = None ) -> NoneType: """ElementHandle.tap This method taps the element by performing the following steps: 1. Wait for [actionability](./actionability.md) checks on the element, unless `force` option is set. 1. Scroll the element into view if needed. 1. Use `page.touchscreen` to tap the center of the element, or the specified `position`. 1. Wait for initiated navigations to either succeed or fail, unless `noWaitAfter` option is set. If the element is detached from the DOM at any moment during the action, this method rejects. When all steps combined have not finished during the specified `timeout`, this method rejects with a `TimeoutError`. Passing zero timeout disables this. > NOTE: `elementHandle.tap()` requires that the `hasTouch` option of the browser context be set to true. Parameters ---------- modifiers : Union[List[Union["Alt", "Control", "Meta", "Shift"]], NoneType] Modifier keys to press. Ensures that only these modifiers are pressed during the operation, and then restores current modifiers back. If not specified, currently pressed modifiers are used. position : Union[{x: float, y: float}, NoneType] A point to use relative to the top-left corner of element padding box. If not specified, uses some visible point of the element. timeout : Union[float, NoneType] Maximum time in milliseconds, defaults to 30 seconds, pass `0` to disable timeout. The default value can be changed by using the `browser_context.set_default_timeout()` or `page.set_default_timeout()` methods. force : Union[bool, NoneType] Whether to bypass the [actionability](./actionability.md) checks. Defaults to `false`. no_wait_after : Union[bool, NoneType] Actions that initiate navigations are waiting for these navigations to happen and for pages to start loading. You can opt out of waiting via setting this flag. You would only need this option in the exceptional cases such as navigating to inaccessible pages. Defaults to `false`. """ return mapping.from_maybe_impl( self._sync( "element_handle.tap", self._impl_obj.tap( modifiers=modifiers, position=position, timeout=timeout, force=force, noWaitAfter=no_wait_after, ), ) ) def fill( self, value: str, *, timeout: float = None, no_wait_after: bool = None ) -> NoneType: """ElementHandle.fill This method waits for [actionability](./actionability.md) checks, focuses the element, fills it and triggers an `input` event after filling. If the element is inside the `