MCPcopy Create free account
hub / github.com/THUDM/AutoWebGLM / execute_focus

Function execute_focus

webarena/browser_env/actions.py:1050–1083  ·  view source on GitHub ↗

Click the specified DOM element.

(
    element_role: int, element_name: str, nth: int, page: Page
)

Source from the content-addressed store, hash-verified

1048
1049
1050def 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
1086async def aexecute_focus(

Callers 1

execute_actionFunction · 0.85

Calls 1

is_in_viewportFunction · 0.85

Tested by

no test coverage detected