Click the specified DOM element.
(
element_role: int, element_name: str, nth: int, page: Page
)
| 1048 | |
| 1049 | |
| 1050 | def execute_focus( |
| 1051 | element_role: int, element_name: str, nth: int, page: Page |
| 1052 | ) -> None: |
| 1053 | """Click the specified DOM element.""" |
| 1054 | element_role_str = _id2role[element_role] |
| 1055 | if page.viewport_size is None: |
| 1056 | raise ValueError("Viewport size is not set for the current page") |
| 1057 | element_location_list: list[tuple[Locator, float, float]] = [] |
| 1058 | for frame in page.frames: |
| 1059 | match element_role_str: |
| 1060 | case "alt_text": |
| 1061 | locators = frame.get_by_alt_text(element_name) |
| 1062 | case "label": |
| 1063 | locators = frame.get_by_label(element_name) |
| 1064 | case "placeholder": |
| 1065 | locators = frame.get_by_placeholder(element_name) |
| 1066 | case _: |
| 1067 | locators = frame.get_by_role( |
| 1068 | role=element_role_str, name=element_name |
| 1069 | ) |
| 1070 | for locator_idx in range(locators.count()): |
| 1071 | locator = locators.nth(locator_idx) |
| 1072 | if is_in_viewport(locator, page.viewport_size): |
| 1073 | bounding_box = locator.bounding_box() |
| 1074 | assert bounding_box |
| 1075 | element_location_list.append( |
| 1076 | (locator, bounding_box["x"], bounding_box["y"]) |
| 1077 | ) |
| 1078 | if len(element_location_list) <= nth: |
| 1079 | raise ValueError( |
| 1080 | f"There are only {len(element_location_list)} elements found in viewport, but {nth + 1} is requested" |
| 1081 | ) |
| 1082 | element_location_list.sort(key=lambda x: (x[2], x[1])) # row major order |
| 1083 | element_location_list[nth][0].focus() |
| 1084 | |
| 1085 | |
| 1086 | async def aexecute_focus( |
no test coverage detected