MCPcopy Create free account
hub / github.com/THUDM/AgentTuning / aexecute_focus

Function aexecute_focus

eval_heldout/webarena/browser_env/actions.py:944–977  ·  view source on GitHub ↗

Click the specified DOM element.

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

Source from the content-addressed store, hash-verified

942
943@beartype
944async def aexecute_focus(
945 element_role: int, element_name: str, nth: int, page: APage
946) -> None:
947 """Click the specified DOM element."""
948 element_role_str = _id2role[element_role]
949 if page.viewport_size is None:
950 raise ValueError("Viewport size is not set for the current page")
951 element_location_list: list[tuple[ALocator, float, float]] = []
952 for frame in page.frames:
953 match element_role_str:
954 case "alt_text":
955 locators = frame.get_by_alt_text(element_name)
956 case "label":
957 locators = frame.get_by_label(element_name)
958 case "placeholder":
959 locators = frame.get_by_placeholder(element_name)
960 case _:
961 locators = frame.get_by_role(
962 role=element_role_str, name=element_name
963 )
964 for locator_idx in range(await locators.count()):
965 locator = locators.nth(locator_idx)
966 if await async_is_in_viewport(locator, page.viewport_size):
967 bounding_box = await locator.bounding_box()
968 assert bounding_box
969 element_location_list.append(
970 (locator, bounding_box["x"], bounding_box["y"])
971 )
972 if len(element_location_list) <= nth:
973 raise ValueError(
974 f"There are only {len(element_location_list)} elements found in viewport, but {nth + 1} is requested"
975 )
976 element_location_list.sort(key=lambda x: (x[2], x[1])) # row major order
977 await element_location_list[nth][0].focus()
978
979
980@beartype

Callers 1

aexecute_actionFunction · 0.85

Calls 2

async_is_in_viewportFunction · 0.85
countMethod · 0.80

Tested by

no test coverage detected