def create_playwright_action()

in evals/elsuite/multistep_web_tasks/webarena/browser_env/actions.py [0:0]


def create_playwright_action(playwright_code: str) -> BrowserAction:
    """Main function to return individual playwright action"""
    # get the last action
    regex = r"\.(?![^\(\)]*\))"
    action = re.split(regex, playwright_code)[-1].split("(")[0]
    # used to be match statement
    if action == "press":
        p = r'press\((?:"|\')(.+?)(?:"|\')\)'
        match = re.search(p, playwright_code)
        if not match:
            raise ActionParsingError(
                "Invalid press action, required to be page.press(KEY_COMB_STR)"
            )
        key_comb = match.group(1)
        return create_key_press_action(key_comb=key_comb)
    elif action == "scroll":
        direction = "up" if "up" in playwright_code else "down"
        return create_scroll_action(direction=direction)
    elif action == "click":
        return create_click_action(pw_code=playwright_code)
    elif action == "hover":
        return create_hover_action(pw_code=playwright_code)
    elif action in ["type", "fill"]:
        p = r'type|fill\((?:"|\')(.+?)(?:"|\')\)'
        match = re.search(p, playwright_code)
        if not match:
            raise ActionParsingError("Invalid type/fill action, required to be page.type(TEXT)")
        text = match.group(1)
        return create_type_action(text=text, pw_code=playwright_code)
    elif action == "select_option":
        return create_select_option_action(pw_code=playwright_code)
    elif action == "check":
        return create_check_action(pw_code=playwright_code)
    elif action == "goto":
        p = r'goto\((?:"|\')(.+?)(?:"|\')\)'
        match = re.search(p, playwright_code)
        if not match:
            raise ActionParsingError("Invalid goto action, required to be page.goto(URL_STR)")
        url = match.group(1)
        return create_goto_url_action(url)
    elif action == "page_focus":
        # get the page number
        p = r"page_focus\((\d+)\)"
        match = re.search(p, playwright_code)
        if not match:
            raise ActionParsingError("page focus requires a page number")
        page_num = int(match.group(1))
        return create_page_focus_action(page_num)
    elif action == "new_tab":
        return create_new_tab_action()
    elif action == "go_back":
        return create_go_back_action()
    elif action == "go_forward":
        return create_go_forward_action()
    elif action == "page_close":
        return create_page_close_action()
    elif action == "stop":  # page.stop(answer)
        p = r'stop\(?"(.+)?"\)'
        match = re.search(p, playwright_code)
        if not match:
            answer = ""
        else:
            answer = match.group(1)
        return create_stop_action(answer)

    raise ActionParsingError(f"Unknown playwright action {action}")