| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466 |
- # 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 base64
- import mimetypes
- from pathlib import Path
- from typing import (
- TYPE_CHECKING,
- Any,
- Callable,
- Dict,
- List,
- Literal,
- Optional,
- Sequence,
- Union,
- cast,
- )
- from playwright._impl._api_structures import FilePayload, FloatRect, Position
- from playwright._impl._connection import ChannelOwner, from_nullable_channel
- from playwright._impl._helper import (
- Error,
- KeyboardModifier,
- MouseButton,
- async_writefile,
- locals_to_params,
- make_dirs_for_file,
- )
- from playwright._impl._js_handle import (
- JSHandle,
- Serializable,
- parse_result,
- serialize_argument,
- )
- from playwright._impl._set_input_files_helpers import convert_input_files
- if TYPE_CHECKING: # pragma: no cover
- from playwright._impl._frame import Frame
- from playwright._impl._locator import Locator
- class ElementHandle(JSHandle):
- def __init__(
- self, parent: ChannelOwner, type: str, guid: str, initializer: Dict
- ) -> None:
- super().__init__(parent, type, guid, initializer)
- self._frame = cast("Frame", parent)
- async def _createSelectorForTest(self, name: str) -> Optional[str]:
- return await self._channel.send(
- "createSelectorForTest", self._frame._timeout, dict(name=name)
- )
- def as_element(self) -> Optional["ElementHandle"]:
- return self
- async def owner_frame(self) -> Optional["Frame"]:
- return from_nullable_channel(await self._channel.send("ownerFrame", None))
- async def content_frame(self) -> Optional["Frame"]:
- return from_nullable_channel(await self._channel.send("contentFrame", None))
- async def get_attribute(self, name: str) -> Optional[str]:
- return await self._channel.send("getAttribute", None, dict(name=name))
- async def text_content(self) -> Optional[str]:
- return await self._channel.send("textContent", None)
- async def inner_text(self) -> str:
- return await self._channel.send("innerText", None)
- async def inner_html(self) -> str:
- return await self._channel.send("innerHTML", None)
- async def is_checked(self) -> bool:
- return await self._channel.send("isChecked", None)
- async def is_disabled(self) -> bool:
- return await self._channel.send("isDisabled", None)
- async def is_editable(self) -> bool:
- return await self._channel.send("isEditable", None)
- async def is_enabled(self) -> bool:
- return await self._channel.send("isEnabled", None)
- async def is_hidden(self) -> bool:
- return await self._channel.send("isHidden", None)
- async def is_visible(self) -> bool:
- return await self._channel.send("isVisible", None)
- async def dispatch_event(self, type: str, eventInit: Dict = None) -> None:
- await self._channel.send(
- "dispatchEvent",
- None,
- dict(type=type, eventInit=serialize_argument(eventInit)),
- )
- async def scroll_into_view_if_needed(self, timeout: float = None) -> None:
- await self._channel.send(
- "scrollIntoViewIfNeeded", self._frame._timeout, locals_to_params(locals())
- )
- async def hover(
- self,
- modifiers: Sequence[KeyboardModifier] = None,
- position: Position = None,
- timeout: float = None,
- noWaitAfter: bool = None,
- force: bool = None,
- trial: bool = None,
- ) -> None:
- await self._channel.send(
- "hover", self._frame._timeout, locals_to_params(locals())
- )
- async def click(
- self,
- modifiers: Sequence[KeyboardModifier] = None,
- position: Position = None,
- delay: float = None,
- button: MouseButton = None,
- clickCount: int = None,
- timeout: float = None,
- force: bool = None,
- noWaitAfter: bool = None,
- trial: bool = None,
- steps: int = None,
- ) -> None:
- await self._channel.send(
- "click", self._frame._timeout, locals_to_params(locals())
- )
- async def dblclick(
- self,
- modifiers: Sequence[KeyboardModifier] = None,
- position: Position = None,
- delay: float = None,
- button: MouseButton = None,
- timeout: float = None,
- force: bool = None,
- noWaitAfter: bool = None,
- trial: bool = None,
- steps: int = None,
- ) -> None:
- await self._channel.send(
- "dblclick", self._frame._timeout, locals_to_params(locals())
- )
- async def select_option(
- self,
- value: Union[str, Sequence[str]] = None,
- index: Union[int, Sequence[int]] = None,
- label: Union[str, Sequence[str]] = None,
- element: Union["ElementHandle", Sequence["ElementHandle"]] = None,
- timeout: float = None,
- force: bool = None,
- noWaitAfter: bool = None,
- ) -> List[str]:
- params = locals_to_params(
- dict(
- timeout=timeout,
- force=force,
- **convert_select_option_values(value, index, label, element),
- )
- )
- return await self._channel.send("selectOption", self._frame._timeout, params)
- async def tap(
- self,
- modifiers: Sequence[KeyboardModifier] = None,
- position: Position = None,
- timeout: float = None,
- force: bool = None,
- noWaitAfter: bool = None,
- trial: bool = None,
- ) -> None:
- await self._channel.send(
- "tap", self._frame._timeout, locals_to_params(locals())
- )
- async def fill(
- self,
- value: str,
- timeout: float = None,
- noWaitAfter: bool = None,
- force: bool = None,
- ) -> None:
- await self._channel.send(
- "fill", self._frame._timeout, locals_to_params(locals())
- )
- async def select_text(self, force: bool = None, timeout: float = None) -> None:
- await self._channel.send(
- "selectText", self._frame._timeout, locals_to_params(locals())
- )
- async def input_value(self, timeout: float = None) -> str:
- return await self._channel.send(
- "inputValue", self._frame._timeout, locals_to_params(locals())
- )
- async def set_input_files(
- self,
- files: Union[
- str, Path, FilePayload, Sequence[Union[str, Path]], Sequence[FilePayload]
- ],
- timeout: float = None,
- noWaitAfter: bool = None,
- ) -> None:
- frame = await self.owner_frame()
- if not frame:
- raise Error("Cannot set input files to detached element")
- converted = await convert_input_files(files, frame.page.context)
- await self._channel.send(
- "setInputFiles",
- self._frame._timeout,
- {
- "timeout": timeout,
- **converted,
- },
- )
- async def focus(self) -> None:
- await self._channel.send("focus", None)
- async def type(
- self,
- text: str,
- delay: float = None,
- timeout: float = None,
- noWaitAfter: bool = None,
- ) -> None:
- await self._channel.send(
- "type", self._frame._timeout, locals_to_params(locals())
- )
- async def press(
- self,
- key: str,
- delay: float = None,
- timeout: float = None,
- noWaitAfter: bool = None,
- ) -> None:
- await self._channel.send(
- "press", self._frame._timeout, locals_to_params(locals())
- )
- async def set_checked(
- self,
- checked: bool,
- position: Position = None,
- timeout: float = None,
- force: bool = None,
- noWaitAfter: bool = None,
- trial: bool = None,
- ) -> None:
- if checked:
- await self.check(
- position=position,
- timeout=timeout,
- force=force,
- trial=trial,
- )
- else:
- await self.uncheck(
- position=position,
- timeout=timeout,
- force=force,
- trial=trial,
- )
- async def check(
- self,
- position: Position = None,
- timeout: float = None,
- force: bool = None,
- noWaitAfter: bool = None,
- trial: bool = None,
- ) -> None:
- await self._channel.send(
- "check", self._frame._timeout, locals_to_params(locals())
- )
- async def uncheck(
- self,
- position: Position = None,
- timeout: float = None,
- force: bool = None,
- noWaitAfter: bool = None,
- trial: bool = None,
- ) -> None:
- await self._channel.send(
- "uncheck", self._frame._timeout, locals_to_params(locals())
- )
- async def bounding_box(self) -> Optional[FloatRect]:
- return await self._channel.send("boundingBox", None)
- async def screenshot(
- self,
- timeout: float = None,
- type: Literal["jpeg", "png"] = None,
- path: Union[str, Path] = None,
- quality: int = None,
- omitBackground: bool = None,
- animations: Literal["allow", "disabled"] = None,
- caret: Literal["hide", "initial"] = None,
- scale: Literal["css", "device"] = None,
- mask: Sequence["Locator"] = None,
- maskColor: str = None,
- style: str = None,
- ) -> bytes:
- params = locals_to_params(locals())
- if "path" in params:
- if "type" not in params:
- params["type"] = determine_screenshot_type(params["path"])
- del params["path"]
- if "mask" in params:
- params["mask"] = list(
- map(
- lambda locator: (
- {
- "frame": locator._frame._channel,
- "selector": locator._selector,
- }
- ),
- params["mask"],
- )
- )
- encoded_binary = await self._channel.send(
- "screenshot", self._frame._timeout, params
- )
- decoded_binary = base64.b64decode(encoded_binary)
- if path:
- make_dirs_for_file(path)
- await async_writefile(path, decoded_binary)
- return decoded_binary
- async def query_selector(self, selector: str) -> Optional["ElementHandle"]:
- return from_nullable_channel(
- await self._channel.send("querySelector", None, dict(selector=selector))
- )
- async def query_selector_all(self, selector: str) -> List["ElementHandle"]:
- return list(
- map(
- cast(Callable[[Any], Any], from_nullable_channel),
- await self._channel.send(
- "querySelectorAll", None, dict(selector=selector)
- ),
- )
- )
- async def eval_on_selector(
- self,
- selector: str,
- expression: str,
- arg: Serializable = None,
- ) -> Any:
- return parse_result(
- await self._channel.send(
- "evalOnSelector",
- None,
- dict(
- selector=selector,
- expression=expression,
- arg=serialize_argument(arg),
- ),
- )
- )
- async def eval_on_selector_all(
- self,
- selector: str,
- expression: str,
- arg: Serializable = None,
- ) -> Any:
- return parse_result(
- await self._channel.send(
- "evalOnSelectorAll",
- None,
- dict(
- selector=selector,
- expression=expression,
- arg=serialize_argument(arg),
- ),
- )
- )
- async def wait_for_element_state(
- self,
- state: Literal[
- "disabled", "editable", "enabled", "hidden", "stable", "visible"
- ],
- timeout: float = None,
- ) -> None:
- await self._channel.send(
- "waitForElementState", self._frame._timeout, locals_to_params(locals())
- )
- async def wait_for_selector(
- self,
- selector: str,
- state: Literal["attached", "detached", "hidden", "visible"] = None,
- timeout: float = None,
- strict: bool = None,
- ) -> Optional["ElementHandle"]:
- return from_nullable_channel(
- await self._channel.send(
- "waitForSelector", self._frame._timeout, locals_to_params(locals())
- )
- )
- def convert_select_option_values(
- value: Union[str, Sequence[str]] = None,
- index: Union[int, Sequence[int]] = None,
- label: Union[str, Sequence[str]] = None,
- element: Union["ElementHandle", Sequence["ElementHandle"]] = None,
- ) -> Any:
- if value is None and index is None and label is None and element is None:
- return {}
- options: Any = None
- elements: Any = None
- if value is not None:
- if isinstance(value, str):
- value = [value]
- options = (options or []) + list(map(lambda e: dict(valueOrLabel=e), value))
- if index is not None:
- if isinstance(index, int):
- index = [index]
- options = (options or []) + list(map(lambda e: dict(index=e), index))
- if label is not None:
- if isinstance(label, str):
- label = [label]
- options = (options or []) + list(map(lambda e: dict(label=e), label))
- if element:
- if isinstance(element, ElementHandle):
- element = [element]
- elements = list(map(lambda e: e._channel, element))
- return dict(options=options, elements=elements)
- def determine_screenshot_type(path: Union[str, Path]) -> Literal["jpeg", "png"]:
- mime_type, _ = mimetypes.guess_type(path)
- if mime_type == "image/png":
- return "png"
- if mime_type == "image/jpeg":
- return "jpeg"
- raise Error(f'Unsupported screenshot mime type for path "{path}": {mime_type}')
|