close select2 when failed to select (#498)
This commit is contained in:
@@ -522,31 +522,37 @@ async def handle_select_option_action(
|
|||||||
if action.option.index is not None:
|
if action.option.index is not None:
|
||||||
if action.option.index >= len(options):
|
if action.option.index >= len(options):
|
||||||
result.append(ActionFailure(Exception("Select index out of bound")))
|
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:
|
await select2_element.select_by_index(index=action.option.index, timeout=timeout)
|
||||||
option_content = options[action.option.index].get("text")
|
result.append(ActionSuccess())
|
||||||
if option_content != action.option.label:
|
return result
|
||||||
LOG.warning(
|
except Exception as e:
|
||||||
"Select option label is not consistant to the action value. Might select wrong option.",
|
result.append(ActionFailure(e))
|
||||||
option_content=option_content,
|
LOG.info(
|
||||||
|
"failed to select by index in select2, try to select by label",
|
||||||
|
exc_info=True,
|
||||||
action=action,
|
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:
|
if len(result) == 0:
|
||||||
result.append(ActionFailure(Exception("nothing is selected, try to select again.")))
|
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
|
return result
|
||||||
elif tag_name == "ul" or tag_name == "div" or tag_name == "li":
|
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
|
# if the role is listbox, find the option with the "label" or "value" and click that option element
|
||||||
|
|||||||
@@ -167,6 +167,9 @@ class Select2Dropdown:
|
|||||||
# wait for the options to load
|
# wait for the options to load
|
||||||
await asyncio.sleep(3)
|
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]:
|
async def get_options(self) -> typing.List[SkyvernOptionType]:
|
||||||
options = await get_select2_options(self.page)
|
options = await get_select2_options(self.page)
|
||||||
return typing.cast(typing.List[SkyvernOptionType], options)
|
return typing.cast(typing.List[SkyvernOptionType], options)
|
||||||
|
|||||||
Reference in New Issue
Block a user