Skip to content

Commit

Permalink
close select2 when failed to select (#498)
Browse files Browse the repository at this point in the history
  • Loading branch information
LawyZheng committed Jun 21, 2024
1 parent 0ab41a2 commit 49d7e77
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
42 changes: 24 additions & 18 deletions skyvern/webeye/actions/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions skyvern/webeye/utils/dom.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ async def open(self, timeout: float = SettingsManager.get_settings().BROWSER_ACT
# 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)
Expand Down

0 comments on commit 49d7e77

Please sign in to comment.