Click Element
The Click Element action searches for a UI element using its visible text and performs a click action. This is one of the most commonly used automation actions and is suitable for interacting with buttons, menu items, dialog options, list items, and other clickable controls.
When to Use
Use this action when you want to:
- Tap an OK button.
- Select a menu item.
- Open a settings page.
- Accept a permission dialog.
- Continue through an application setup wizard.
- Click any visible button or option within an application.
Syntax
var task = suremdmjs.automate().click(
"<Package Name>",
"<Element Text>"
);
suremdmjs.automate().performAction(
task,
"onSuccess",
"onFailure"
);
Parameters
| Parameter | Required |
|---|---|
| Package Name | Yes |
| Element Text | Yes |
Parameter Details
Package Name
Specifies the Android package name of the application containing the target UI element. Providing the package name limits the search to a specific application, improving both performance and accuracy.
Example:
com.android.settings
To search all currently visible applications, pass an empty string.
""
Element Text
Specifies the exact visible text displayed on the UI element.
Example values:
OK
Allow
Continue
Install
Settings
The automation searches for the first matching visible element and performs the click action.
Example
The following example clicks the Display option in the Android Settings application.
!#suremdmjs
openDisplaySettings();
function openDisplaySettings() {
var task = suremdmjs.automate().click(
"com.android.settings",
"Display"
);
suremdmjs.automate().performAction(
task,
"onDisplayOpened",
"onFailure"
);
}
function onDisplayOpened() {
suremdmjs.log("Display settings opened successfully.");
}
function onFailure() {
suremdmjs.log("Display option not found.");
}
How This Example Works
- Creates a Click Element task.
- Searches for the Display option within the Android Settings application.
- Clicks the Display option when found.
- Executes the success callback after the click is completed.
- Executes the failure callback if the element cannot be located.
Expected Result
The Display settings page opens.
If multiple UI elements contain the same visible text, the first matching element identified by the automation engine is clicked.