Click the specified DOM element.
(
element_role: int, element_name: str, nth: int, page: APage
)
| 1084 | |
| 1085 | |
| 1086 | async def aexecute_focus( |
| 1087 | element_role: int, element_name: str, nth: int, page: APage |
| 1088 | ) -> None: |
| 1089 | """Click the specified DOM element.""" |
| 1090 | element_role_str = _id2role[element_role] |
| 1091 | if page.viewport_size is None: |
| 1092 | raise ValueError("Viewport size is not set for the current page") |
| 1093 | element_location_list: list[tuple[ALocator, float, float]] = [] |
| 1094 | for frame in page.frames: |
| 1095 | match element_role_str: |
| 1096 | case "alt_text": |
| 1097 | locators = frame.get_by_alt_text(element_name) |
| 1098 | case "label": |
| 1099 | locators = frame.get_by_label(element_name) |
| 1100 | case "placeholder": |
| 1101 | locators = frame.get_by_placeholder(element_name) |
| 1102 | case _: |
| 1103 | locators = frame.get_by_role( |
| 1104 | role=element_role_str, name=element_name |
| 1105 | ) |
| 1106 | for locator_idx in range(await locators.count()): |
| 1107 | locator = locators.nth(locator_idx) |
| 1108 | if await async_is_in_viewport(locator, page.viewport_size): |
| 1109 | bounding_box = await locator.bounding_box() |
| 1110 | assert bounding_box |
| 1111 | element_location_list.append( |
| 1112 | (locator, bounding_box["x"], bounding_box["y"]) |
| 1113 | ) |
| 1114 | if len(element_location_list) <= nth: |
| 1115 | raise ValueError( |
| 1116 | f"There are only {len(element_location_list)} elements found in viewport, but {nth + 1} is requested" |
| 1117 | ) |
| 1118 | element_location_list.sort(key=lambda x: (x[2], x[1])) # row major order |
| 1119 | await element_location_list[nth][0].focus() |
| 1120 | |
| 1121 | |
| 1122 | def locate(locator_calls: list[ParsedPlaywrightCode], page: Page) -> Locator: |
no test coverage detected