Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
feat(SKY-9959): support double-click in element-based click action (#6166)
LawyZheng527d5e3bec16
diff --git a/skyvern/forge/prompts/skyvern/extract-action-static.j2 b/skyvern/forge/prompts/skyvern/extract-action-static.j2index bf1ac47c5..70cdc6092 100644--- a/skyvern/forge/prompts/skyvern/extract-action-static.j2+++ b/skyvern/forge/prompts/skyvern/extract-action-static.j2@@ -28,6 +28,7 @@ Reply in JSON format with the following keys:"repeat": int, // For KEYPRESS only: how many times to press the key in rapid succession without re-evaluating the page between presses. Use this when navigating transient menus (e.g., ArrowDown 3 times to reach an item). Defaults to 1. null if not KEYPRESS action."direction": str, // The direction to scroll for SCROLL action only. Allowed values: "up", "down". null if not SCROLL action."file_url": str, // The url of the file to upload if applicable. This field must be present for UPLOAD_FILE but can also be present for CLICK only if the click is to upload the file. It should be null otherwise.+ "double_click": bool, // For CLICK action only. True only when the element requires a double-click to activate (e.g., opening a file in a file manager, entering edit mode in a spreadsheet cell, or expanding a tree node). Defaults to false for normal clicks. Do not use double-click for regular buttons, links, form elements, clickable grid rows, icons, or dashboard cards."download": bool, // Can only be true for CLICK or SELECT_OPTION actions. If true, the browser will trigger a download by clicking the element. If false, the browser will click the element without triggering a download."option": { // The option to select for SELECT_OPTION action only. null if not SELECT_OPTION action"label": str, // the label of the option if any. MAKE SURE YOU USE THIS LABEL TO SELECT THE OPTION. DO NOT PUT ANYTHING OTHER THAN A VALID OPTION LABEL HEREdiff --git a/skyvern/forge/prompts/skyvern/extract-action.j2 b/skyvern/forge/prompts/skyvern/extract-action.j2index f1cdde83f..f3a4587de 100644--- a/skyvern/forge/prompts/skyvern/extract-action.j2+++ b/skyvern/forge/prompts/skyvern/extract-action.j2@@ -28,6 +28,7 @@ Reply in JSON format with the following keys:"repeat": int, // For KEYPRESS only: how many times to press the key in rapid succession without re-evaluating the page between presses. Use this when navigating transient menus (e.g., ArrowDown 3 times to reach an item). Defaults to 1. null if not KEYPRESS action."direction": str, // The direction to scroll for SCROLL action only. Allowed values: "up", "down". null if not SCROLL action."file_url": str, // The url of the file to upload if applicable. This field must be present for UPLOAD_FILE but can also be present for CLICK only if the click is to upload the file. It should be null otherwise.+ "double_click": bool, // For CLICK action only. True only when the element requires a double-click to activate (e.g., opening a file in a file manager, entering edit mode in a spreadsheet cell, or expanding a tree node). Defaults to false for normal clicks. Do not use double-click for regular buttons, links, form elements, clickable grid rows, icons, or dashboard cards."download": bool, // Can only be true for CLICK or SELECT_OPTION actions. If true, the browser will trigger a download by clicking the element. If false, the browser will click the element without triggering a download."option": { // The option to select for SELECT_OPTION action only. null if not SELECT_OPTION action"label": str, // the label of the option if any. MAKE SURE YOU USE THIS LABEL TO SELECT THE OPTION. DO NOT PUT ANYTHING OTHER THAN A VALID OPTION LABEL HEREdiff --git a/skyvern/forge/prompts/skyvern/single-click-action.j2 b/skyvern/forge/prompts/skyvern/single-click-action.j2index fa56577d4..07cde1a19 100644--- a/skyvern/forge/prompts/skyvern/single-click-action.j2+++ b/skyvern/forge/prompts/skyvern/single-click-action.j2@@ -14,6 +14,7 @@ Reply in JSON format with the following keys:"confidence_float": float, // The confidence of the action. Pick a number between 0.0 and 1.0. 0.0 means no confidence, 1.0 means full confidence"action_type": str, // It's a string enum: "CLICK". "CLICK" type means there's an element you'd like to click."id": str, // The id of the element to take action on. The id has to be one from the elements list.+ "double_click": bool, // True only when the element requires a double-click to activate (e.g., opening a file in a file manager, entering edit mode in a spreadsheet cell, or expanding a tree node). Defaults to false for normal clicks. Do not use double-click for regular buttons, links, form elements, clickable grid rows, icons, or dashboard cards."download": bool, // If true, the browser will trigger a download by clicking the element. If false, the browser will click the element without triggering a download."file_url": str, // The url of the file to upload if applicable. This field can be present for CLICK only if the click is to upload the file. It should be null otherwise.}]diff --git a/skyvern/webeye/actions/actions.py b/skyvern/webeye/actions/actions.pyindex 35b4850f3..88fdea879 100644--- a/skyvern/webeye/actions/actions.py+++ b/skyvern/webeye/actions/actions.py@@ -264,7 +264,7 @@ class ClickAction(WebAction):repeat: int = 1def __repr__(self) -> str:- return f"ClickAction(element_id={self.element_id}, file_url={self.file_url}, download={self.download}, x={self.x}, y={self.y}, button={self.button}, tool_call_id={self.tool_call_id})"+ return f"ClickAction(element_id={self.element_id}, file_url={self.file_url}, download={self.download}, x={self.x}, y={self.y}, button={self.button}, repeat={self.repeat}, tool_call_id={self.tool_call_id})"class InputTextAction(WebAction):diff --git a/skyvern/webeye/actions/handler.py b/skyvern/webeye/actions/handler.pyindex 1bf63b137..f09de0f15 100644--- a/skyvern/webeye/actions/handler.py+++ b/skyvern/webeye/actions/handler.py@@ -3131,6 +3131,26 @@ async def _did_page_respond(return True+def _get_click_count(action: ClickAction | UploadFileAction) -> int:+ if isinstance(action, ClickAction):+ return action.repeat+ return 1+++async def _locator_click(+ locator: Locator,+ click_count: int,+ timeout: int = settings.BROWSER_ACTION_TIMEOUT_MS,+ **kwargs: Any,+) -> None:+ if click_count == 2:+ await locator.dblclick(timeout=timeout, **kwargs)+ elif click_count >= 3:+ await locator.click(timeout=timeout, click_count=click_count, **kwargs)+ else:+ await locator.click(timeout=timeout, **kwargs)++async def chain_click(task: Task,scraped_page: ScrapedPage,@@ -3148,6 +3168,7 @@ async def chain_click(dom = DomUtil(scraped_page=scraped_page, page=page)locator = skyvern_element.locator+ click_count = _get_click_count(action)# TODO (suchintan): This should likely result in an ActionFailure -- we can figure out how to do this later!LOG.info("Chain click starts", action=action, locator=locator)file = pending_upload_files or []@@ -3190,7 +3211,7 @@ async def chain_click(try:if not await skyvern_element.navigate_to_a_href(page=page):await EventStrategyFactory.move_to_element(page, locator)- await locator.click(timeout=timeout)+ await _locator_click(locator, click_count, timeout=timeout)LOG.info("Chain click: main element click succeeded", action=action, locator=locator)action_results = [ActionSuccess()]return action_results@@ -3207,7 +3228,7 @@ async def chain_click(locator=locator,)if bound_element := await skyvern_element.find_label_for(dom=dom):- await bound_element.get_locator().click(timeout=timeout)+ await _locator_click(bound_element.get_locator(), click_count, timeout=timeout)action_results.append(ActionSuccess())return action_resultsexcept Exception as e:@@ -3225,7 +3246,7 @@ async def chain_click(if bound_element := await skyvern_element.find_element_in_label_children(dom=dom, element_type=InteractiveElement.INPUT):- await bound_element.get_locator().click(timeout=timeout)+ await _locator_click(bound_element.get_locator(), click_count, timeout=timeout)action_results.append(ActionSuccess())return action_resultsexcept Exception as e:@@ -3243,7 +3264,7 @@ async def chain_click()if bound_locator := await skyvern_element.find_bound_label_by_attr_id():# click on (0, 0) to avoid playwright clicking on the wrong element by accident- await bound_locator.click(timeout=timeout, position={"x": 0, "y": 0})+ await _locator_click(bound_locator, click_count, timeout=timeout, position={"x": 0, "y": 0})action_results.append(ActionSuccess())return action_resultsexcept Exception as e:@@ -3260,7 +3281,7 @@ async def chain_click()if bound_locator := await skyvern_element.find_bound_label_by_direct_parent():# click on (0, 0) to avoid playwright clicking on the wrong element by accident- await bound_locator.click(timeout=timeout, position={"x": 0, "y": 0})+ await _locator_click(bound_locator, click_count, timeout=timeout, position={"x": 0, "y": 0})action_results.append(ActionSuccess())return action_resultsexcept Exception as e:@@ -3300,7 +3321,7 @@ async def chain_click()try:- await skyvern_element.coordinate_click(page=page)+ await skyvern_element.coordinate_click(page=page, click_count=click_count)action_results.append(ActionSuccess())return action_resultsexcept Exception as e:diff --git a/skyvern/webeye/actions/parse_actions.py b/skyvern/webeye/actions/parse_actions.pyindex 31eb35b6f..883d3d351 100644--- a/skyvern/webeye/actions/parse_actions.py+++ b/skyvern/webeye/actions/parse_actions.py@@ -145,11 +145,13 @@ def parse_action(click_context = action.get("click_context", None)if click_context:click_context = ClickContext.model_validate(click_context)+ double_click = action.get("double_click", False)return ClickAction(**base_action_dict,file_url=file_url,download=action.get("download", False),click_context=click_context,+ repeat=2 if double_click else 1,)if action_type == ActionType.INPUT_TEXT:diff --git a/skyvern/webeye/utils/dom.py b/skyvern/webeye/utils/dom.pyindex a8b1b13ac..6c648a696 100644--- a/skyvern/webeye/utils/dom.py+++ b/skyvern/webeye/utils/dom.py@@ -883,9 +883,16 @@ class SkyvernElement:skyvern_frame = await SkyvernFrame.create_instance(self.get_frame())await skyvern_frame.click_element_in_javascript(await self.get_element_handler())- async def coordinate_click(self, page: Page, timeout: float = settings.BROWSER_ACTION_TIMEOUT_MS) -> None:+ async def coordinate_click(+ self, page: Page, timeout: float = settings.BROWSER_ACTION_TIMEOUT_MS, click_count: int = 1+ ) -> None:click_x, click_y = await self.move_mouse_to(page=page, timeout=timeout)- await page.mouse.click(click_x, click_y)+ if click_count == 2:+ await page.mouse.dblclick(click_x, click_y)+ elif click_count >= 3:+ await page.mouse.click(click_x, click_y, click_count=click_count)+ else:+ await page.mouse.click(click_x, click_y)async def blur(self) -> None:if not await self.is_visible():diff --git a/tests/unit/test_actions.py b/tests/unit/test_actions.pyindex d61d631ea..dacb0002f 100644--- a/tests/unit/test_actions.py+++ b/tests/unit/test_actions.py@@ -3,7 +3,7 @@ from unittest.mock import MagicMockimport pytestfrom pydantic import ValidationError-from skyvern.webeye.actions.actions import Action, KeypressAction, NullAction, WebAction+from skyvern.webeye.actions.actions import Action, ClickAction, KeypressAction, NullAction, WebActionfrom skyvern.webeye.actions.parse_actions import parse_action@@ -141,3 +141,30 @@ def test_parse_keypress_repeat_clamped_to_minimum_one() -> None:)assert isinstance(action, KeypressAction)assert action.repeat == 1+++def test_parse_click_double_click_true() -> None:+ action = parse_action(+ action={"action_type": "CLICK", "id": "1", "reasoning": "test", "double_click": True},+ scraped_page=_mock_scraped_page(),+ )+ assert isinstance(action, ClickAction)+ assert action.repeat == 2+++def test_parse_click_double_click_false() -> None:+ action = parse_action(+ action={"action_type": "CLICK", "id": "1", "reasoning": "test", "double_click": False},+ scraped_page=_mock_scraped_page(),+ )+ assert isinstance(action, ClickAction)+ assert action.repeat == 1+++def test_parse_click_no_double_click_field() -> None:+ action = parse_action(+ action={"action_type": "CLICK", "id": "1", "reasoning": "test"},+ scraped_page=_mock_scraped_page(),+ )+ assert isinstance(action, ClickAction)+ assert action.repeat == 1