def create_id_based_action()

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


def create_id_based_action(action_str: str) -> BrowserAction:
    """Main function to return individual id based action"""
    action_str = action_str.strip()
    action = (
        action_str.split("[")[0].strip() if "[" in action_str else action_str.split()[0].strip()
    )
    # used to be match statement
    if action == "click":
        match = re.search(r"click ?\[(\d+)\]", action_str)
        if not match:
            raise ActionParsingError(f"Invalid click action {action_str}")
        element_id = match.group(1)
        return create_click_action(element_id=element_id)
    elif action == "hover":
        match = re.search(r"hover ?\[(\d+)\]", action_str)
        if not match:
            raise ActionParsingError(f"Invalid hover action {action_str}")
        element_id = match.group(1)
        return create_hover_action(element_id=element_id)
    elif action == "type":
        # add default enter flag
        if not (action_str.endswith("[0]") or action_str.endswith("[1]")):
            action_str += " [1]"

        match = re.search(r"type ?\[(\d+)\] ?\[(.+)\] ?\[(\d+)\]", action_str)
        if not match:
            raise ActionParsingError(f"Invalid type action {action_str}")
        element_id, text, enter_flag = (
            match.group(1),
            match.group(2),
            match.group(3),
        )
        if enter_flag == "1":
            text += "\n"
        return create_type_action(text=text, element_id=element_id)
    elif action == "press":
        match = re.search(r"press ?\[(.+)\]", action_str)
        if not match:
            raise ActionParsingError(f"Invalid press action {action_str}")
        key_comb = match.group(1)
        return create_key_press_action(key_comb=key_comb)
    elif action == "scroll":
        # up or down
        match = re.search(r"scroll ?\[?(up|down)\]?", action_str)
        if not match:
            raise ActionParsingError(f"Invalid scroll action {action_str}")
        direction = match.group(1)
        return create_scroll_action(direction=direction)
    elif action == "goto":
        match = re.search(r"goto ?\[(.+)\]", action_str)
        if not match:
            raise ActionParsingError(f"Invalid goto action {action_str}")
        url = match.group(1)
        return create_goto_url_action(url=url)
    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 == "tab_focus":
        match = re.search(r"tab_focus ?\[(\d+)\]", action_str)
        if not match:
            raise ActionParsingError(f"Invalid tab_focus action {action_str}")
        page_number = int(match.group(1))
        return create_page_focus_action(page_number)
    elif action == "close_tab":
        return create_page_close_action()
    elif action == "stop":  # stop answer
        match = re.search(r"stop ?\[(.+)\]", action_str)
        if not match:  # some tasks don't require an answer
            answer = ""
        else:
            answer = match.group(1)
        return create_stop_action(answer)
    else:
        raise ActionParsingError(f"Invalid action {action_str}")