click_element
Click element(s) matching a CSS, XPath, or supported text selector.
Instructions
Click element(s) matching a CSS, XPath, or supported text selector.
Use this tool for normal clicks, clicking a specific matching occurrence, clicking all visible matches, conditional clicks, or clicks scoped to a parent element.
Selection behavior:
- nth is 1-based and takes precedence over every other click mode.
- Otherwise, all_matches=True clicks every currently visible match.
- Otherwise, only_if_visible=True clicks only if a match is visible.
- Otherwise, parent_selector scopes the click to a nested element.
- With none of the above, performs a normal SeleniumBase click.
Args:
selector: CSS selector, XPath selector, or supported SeleniumBase
text-matching selector. Text-matching selectors such as
a:contains("Sign in") are supported only for single-element
clicks; do not use them with all_matches=True.
nth: 1-based occurrence to click when multiple elements match.
Must be >= 1 if provided. Takes precedence over `all_matches`,
`only_if_visible`, and `parent_selector`.
all_matches: If True, click every currently visible matching element
in order of appearance. Ignored when `nth` is provided. Use only
when multiple clicks are intentionally desired, such as for
clicking all the checkboxes in a section of a webpage.
If any of the click actions induces page navigation, then
subsequent clicks are cancelled without any exceptions raised.
only_if_visible: If True, click only when the target is already
visible; do not wait for it to become visible.
parent_selector: CSS/XPath selector for the parent/container in which
to find `selector`. Used only for the nested-click mode.
Can be used to click an element inside a parent iframe.
timeout: Maximum seconds to wait for a normal click operation.
Default: 5. Not used by conditional or bulk click modes.
scroll: If True, scroll the target into view before a normal or
indexed click. Default: True.Examples:
- Click the first button: click_element("button")
- Click the 2nd button: click_element("button", nth=2)
- Click all checkboxes:
click_element('input[type="checkbox"]', all_matches=True)
- Click the first visible link:
click_element("a", only_if_visible=True)
- Click the first button that's inside the first iframe:
click_element("button", parent_selector="iframe")
Error behavior: With the exception of using 'only_if_visible=True', if there's no matching element found within the timeout, then @handle_sb_errors returns details from the exception raised.
When not to use: - Do not use this tool if you need to hover an element first before clicking; use hover_action with action="hover_and_click" instead.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| nth | No | ||
| scroll | No | ||
| timeout | No | ||
| selector | Yes | ||
| all_matches | No | ||
| only_if_visible | No | ||
| parent_selector | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |