From e5b17bf3b0c73650e5a4f4d0860dab036fcd4b79 Mon Sep 17 00:00:00 2001 From: Nicholas Schwab Date: Sat, 17 Jul 2021 13:31:58 +0200 Subject: [PATCH] Add argument to select first match to click-element. --- doc/help/commands.asciidoc | 3 ++- qutebrowser/components/misccommands.py | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/doc/help/commands.asciidoc b/doc/help/commands.asciidoc index 6003c0f1f..2694dd39d 100644 --- a/doc/help/commands.asciidoc +++ b/doc/help/commands.asciidoc @@ -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 diff --git a/qutebrowser/components/misccommands.py b/qutebrowser/components/misccommands.py index 75ed7c5e2..abd21b2e2 100644 --- a/qutebrowser/components/misccommands.py +++ b/qutebrowser/components/misccommands.py @@ -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]