in evals/elsuite/multistep_web_tasks/webarena/browser_env/actions.py [0:0]
def parse_playwright_code(code: str) -> list[ParsedPlaywrightCode]:
# extract function calls
if not code.startswith("page."):
raise ValueError(f'Playwright action must start with "page.", but got {code}')
regex = r"\.(?![^\(\)]*\))"
chain = re.split(regex, code)[1:]
parsed_chain = []
for item in chain:
tree = ast.parse(item)
funcs = []
for node in ast.walk(tree):
if isinstance(node, ast.Call):
function_name = node.func.id # type: ignore[attr-defined]
arguments = [
ast.literal_eval(arg) if isinstance(arg, ast.Str) else arg for arg in node.args
]
keywords = {str(kw.arg): ast.literal_eval(kw.value) for kw in node.keywords}
funcs.append(
ParsedPlaywrightCode(
{
"function_name": function_name,
"arguments": arguments, # type: ignore (seems to work fine)
"keywords": keywords,
}
)
)
if len(funcs) != 1:
raise ValueError(f"Fail to parse {item} in {code}")
if funcs[0]["function_name"] not in PLAYWRIGHT_LOCATORS + PLAYWRIGHT_ACTIONS:
raise ValueError(
f"Invalid playwright code {item}, ",
f"the function needs to be one of {PLAYWRIGHT_LOCATORS + PLAYWRIGHT_ACTIONS}",
)
parsed_chain.append(funcs[0])
last_action = parsed_chain[-1]
if last_action.data["function_name"] not in PLAYWRIGHT_ACTIONS:
raise ValueError(
f"Invalid playwright action {last_action},",
f"the action needs to be one of {PLAYWRIGHT_ACTIONS}",
)
return parsed_chain