Click the specified DOM element.
(
element_role: int, element_name: str, nth: int, page: Page
)
| 905 | |
| 906 | @beartype |
| 907 | def execute_focus( |
| 908 | element_role: int, element_name: str, nth: int, page: Page |
| 909 | ) -> None: |
| 910 | """Click the specified DOM element.""" |
| 911 | element_role_str = _id2role[element_role] |
| 912 | if page.viewport_size is None: |
| 913 | raise ValueError("Viewport size is not set for the current page") |
| 914 | element_location_list: list[tuple[Locator, float, float]] = [] |
| 915 | for frame in page.frames: |
| 916 | match element_role_str: |
| 917 | case "alt_text": |
| 918 | locators = frame.get_by_alt_text(element_name) |
| 919 | case "label": |
| 920 | locators = frame.get_by_label(element_name) |
| 921 | case "placeholder": |
| 922 | locators = frame.get_by_placeholder(element_name) |
| 923 | case _: |
| 924 | locators = frame.get_by_role( |
| 925 | role=element_role_str, name=element_name |
| 926 | ) |
| 927 | for locator_idx in range(locators.count()): |
| 928 | locator = locators.nth(locator_idx) |
| 929 | if is_in_viewport(locator, page.viewport_size): |
| 930 | bounding_box = locator.bounding_box() |
| 931 | assert bounding_box |
| 932 | element_location_list.append( |
| 933 | (locator, bounding_box["x"], bounding_box["y"]) |
| 934 | ) |
| 935 | if len(element_location_list) <= nth: |
| 936 | raise ValueError( |
| 937 | f"There are only {len(element_location_list)} elements found in viewport, but {nth + 1} is requested" |
| 938 | ) |
| 939 | element_location_list.sort(key=lambda x: (x[2], x[1])) # row major order |
| 940 | element_location_list[nth][0].focus() |
| 941 | |
| 942 | |
| 943 | @beartype |
no test coverage detected