From 49d7e77b3a17d68d718d84cb3d9aa1fa8a0257cd Mon Sep 17 00:00:00 2001 From: LawyZheng Date: Fri, 21 Jun 2024 15:12:26 +0800 Subject: [PATCH] close select2 when failed to select (#498) --- skyvern/webeye/actions/handler.py | 42 ++++++++++++++++++------------- skyvern/webeye/utils/dom.py | 3 +++ 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/skyvern/webeye/actions/handler.py b/skyvern/webeye/actions/handler.py index ba782503..4d3bcd59 100644 --- a/skyvern/webeye/actions/handler.py +++ b/skyvern/webeye/actions/handler.py @@ -522,31 +522,37 @@ async def handle_select_option_action( if action.option.index is not None: if action.option.index >= len(options): result.append(ActionFailure(Exception("Select index out of bound"))) - return result + else: + try: + option_content = options[action.option.index].get("text") + if option_content != action.option.label: + LOG.warning( + "Select option label is not consistant to the action value. Might select wrong option.", + option_content=option_content, + action=action, + ) - try: - option_content = options[action.option.index].get("text") - if option_content != action.option.label: - LOG.warning( - "Select option label is not consistant to the action value. Might select wrong option.", - option_content=option_content, + await select2_element.select_by_index(index=action.option.index, timeout=timeout) + result.append(ActionSuccess()) + return result + except Exception as e: + result.append(ActionFailure(e)) + LOG.info( + "failed to select by index in select2, try to select by label", + exc_info=True, action=action, ) - await select2_element.select_by_index(index=action.option.index, timeout=timeout) - result.append(ActionSuccess()) - return result - except Exception as e: - result.append(ActionFailure(e)) - LOG.info( - "failed to select by index in select2, try to select by label", - exc_info=True, - action=action, - ) - if len(result) == 0: result.append(ActionFailure(Exception("nothing is selected, try to select again."))) + if isinstance(result[-1], ActionFailure): + LOG.info( + "Failed to select a select2 option, close the dropdown", + action=action, + ) + await select2_element.close() + return result elif tag_name == "ul" or tag_name == "div" or tag_name == "li": # if the role is listbox, find the option with the "label" or "value" and click that option element diff --git a/skyvern/webeye/utils/dom.py b/skyvern/webeye/utils/dom.py index 834399fb..0deec3a9 100644 --- a/skyvern/webeye/utils/dom.py +++ b/skyvern/webeye/utils/dom.py @@ -167,6 +167,9 @@ class Select2Dropdown: # wait for the options to load await asyncio.sleep(3) + async def close(self, timeout: float = SettingsManager.get_settings().BROWSER_ACTION_TIMEOUT_MS) -> None: + await self.page.locator("#select2-drop").press("Escape", timeout=timeout) + async def get_options(self) -> typing.List[SkyvernOptionType]: options = await get_select2_options(self.page) return typing.cast(typing.List[SkyvernOptionType], options)