Add argument to select first match to click-element.

This commit is contained in:
Nicholas Schwab 2021-07-17 13:31:58 +02:00
parent 1478a5c276
commit e5b17bf3b0
2 changed files with 6 additions and 3 deletions

View File

@ -254,7 +254,7 @@ Clear all message notifications.
[[click-element]]
=== click-element
Syntax: +:click-element [*--target* 'target'] [*--force-event*] 'filter' 'value'+
Syntax: +:click-element [*--target* 'target'] [*--force-event*] [*--select-first*] 'filter' 'value'+
Click the element matching the given filter.
@ -268,6 +268,7 @@ The given filter needs to result in exactly one element, otherwise, an error is
==== optional arguments
* +*-t*+, +*--target*+: How to open the clicked element (normal/tab/tab-bg/window).
* +*-f*+, +*--force-event*+: Force generating a fake click event.
* +*-s*+, +*--select-first*+: Select first matching element if there are multiple.
[[close]]
=== close

View File

@ -229,7 +229,8 @@ def insert_text(tab: apitypes.Tab, text: str) -> None:
def click_element(tab: apitypes.Tab, filter_: str, value: str, *,
target: apitypes.ClickTarget =
apitypes.ClickTarget.normal,
force_event: bool = False) -> None:
force_event: bool = False,
select_first: bool = False) -> None:
"""Click the element matching the given filter.
The given filter needs to result in exactly one element, otherwise, an
@ -241,6 +242,7 @@ def click_element(tab: apitypes.Tab, filter_: str, value: str, *,
value: The value to filter for.
target: How to open the clicked element (normal/tab/tab-bg/window).
force_event: Force generating a fake click event.
select_first: Select first matching element if there are multiple.
"""
def single_cb(elem: Union[apitypes.WebElement, List[apitypes.WebElement], None]) -> None:
"""Click a single element."""
@ -248,7 +250,7 @@ def click_element(tab: apitypes.Tab, filter_: str, value: str, *,
message.error(f"No element found matching {filter_}={value}!")
return
if isinstance(elem, list):
if len(elem) > 1:
if not select_first and len(elem) > 1:
message.error(f"Multiple elements found matching {filter_}={value}!")
return
elem = elem[0]