Interface Watcher¶
The watcher is used to monitor changes in the current display interface in real time and, when preset conditions are met, execute preset actions such as clicking elements, pressing keys, or recording occurrences. You can think of it as the underlying implementation of functionality like Li Tiaotiao or similar automatic ad-skipping features. Because this feature automatically performs click or key operations, unexpected actions may occur when manual intervention is required, so please use with caution.
Tip
Normally, watchers and event registration should be performed at the very beginning of the script and generally not during execution.
Enable Watcher¶
The following interface will enable the interface watcher. By default, newly registered watchers are in a disabled/off state.
Attention
Before enabling the watcher or a monitoring event, any changes on the interface that match the monitoring conditions will not be automatically processed.
d.set_watcher_loop_enabled(True)
Get Whether Watcher is Enabled¶
You can use the following interface to check whether the interface watcher is currently enabled.
d.get_watcher_loop_enabled()
Disable Watcher¶
After disabling the watcher loop, registered events such as automatic clicks will no longer be automatically processed.
d.set_watcher_loop_enabled(False)
Attention
Registering too many monitoring events may affect system performance or operation real-time performance.
Remove All Watchers¶
Remove all applied monitoring events. This can clear rules applied by scripts that ran previously or were abnormally terminated. It is recommended to execute this once before each script startup to prevent events registered by the previous run or abnormally interrupted script from affecting the current normal processing flow.
d.remove_all_watchers()
Get All Watchers¶
Get all registered watchers, including all states (enabled and disabled watchers).
>>> d.get_watchers()
[name: "ClickAcceptWhenShowAggrement"
, name: "PressBackWhenHomePageShows"
enabled: true
, name: "RecordElementAppearTimes"
enabled: true
]
>>> d.get_watchers()[0].enabled
False
>>>
Get All Enabled Watchers¶
Get all watchers that are in the enabled state.
>>> d.get_enabled_watchers( )
[name: "PressBackWhenHomePageShows"
enabled: true
, name: "RecordElementAppearTimes"
enabled: true
]
>>>
Register Click Event¶
The click event will automatically perform click operations when a matching selector appears on the interface, such as automatically bypassing software pop-ups like user agreements, update prompts, etc., or automatically clicking on certain specific interfaces.
Attention
Before enabling the watcher or a monitoring event, any changes on the interface that match the monitoring conditions will not be automatically processed.
d.register_click_target_selector_watcher("ClickAcceptWhenShowAggrement", [Selector(textContains="用户协议")], Selector(textContains="同意", clickable=True))
Hint
The second parameter supports multiple Selectors, meaning event processing will occur only when all Selector conditions are met.
After registering the above event, when an element containing the text "用户协议" appears on the interface, the "同意" (Agree) button will be automatically clicked.
Register Key Press Event¶
The key press event will automatically perform key operations when a matching selector appears on the interface, such as automatic back press, which can automatically avoid entering certain interfaces. Of course, the same operation can also be achieved using click events.
Attention
Before enabling the watcher or a monitoring event, any changes on the interface that match the monitoring conditions will not be automatically processed.
d.register_press_key_watcher("PressBackWhenHomePageShows", [Selector(textContains="个人中心")], Keys.KEY_HOME)
Hint
The second parameter supports multiple Selectors, meaning event processing will occur only when all Selector conditions are met.
After registering the above event, when an element containing the text "个人中心" appears on the interface, the phone's HOME key will be automatically pressed.
Register Count Event¶
A count event increments the counter by 1 when an operation matching certain selectors appears on the interface. When important information flashes on the screen, you can use this feature to count whether certain interfaces have appeared or the number of times they appear.
Attention
Before enabling the watcher or a monitoring event, any changes on the interface that match the monitoring conditions will not be automatically processed.
d.register_none_op_watcher("RecordElementAppearTimes", [Selector(textContains="好的")])
Hint
The second parameter supports multiple Selectors, meaning event processing will occur only when all Selector conditions are met.
The above event records the number of times an interface containing "好的" appears. Use the following interface to get the trigger count of the count event.
d.get_watcher_triggered_count("RecordElementAppearTimes")
Enable Event¶
After an event is registered, it needs to be enabled to be added to the interface watcher loop; otherwise, it will not be applied.
Attention
Before enabling the watcher or a monitoring event, any changes on the interface that match the monitoring conditions will not be automatically processed.
d.set_watcher_enabled("RecordElementAppearTimes", True)
Disable Event¶
You can call the following interface to make the interface watcher stop monitoring a certain event.
Attention
Before enabling the watcher or a monitoring event, any changes on the interface that match the monitoring conditions will not be automatically processed.
d.set_watcher_enabled("RecordElementAppearTimes", False)
Remove Event¶
Completely remove an event from the watcher; it will also be deleted from registered events.
d.remove_watcher(name)
Get Whether Event is Enabled¶
Call the following interface to check whether the event is properly enabled, to prevent cases where it is registered but not enabled.
d.get_watcher_enabled(name)
Advanced Selector¶
In addition to simple selectors, you can also use more complex selectors for element matching. The following example shows how to write a complex selector.
d.register_press_key_watcher("PressBackWhenHomePageShows", [Selector(resourceId="com.android.market:id/v_app_item").child(index=3).sibling(index=2).child(index=2).child()], Keys.KEY_HOME)
Example Complete Code¶
The following is an example code block that enables the watcher loop and registers three types of watcher events. When text containing "用户协议" appears on the interface, automatically click "同意"; when text containing "个人中心" appears, immediately return to the desktop; when text containing "好的" appears, record the number of times that interface appears.
Attention
Before enabling the watcher or a monitoring event, any changes on the interface that match the monitoring conditions will not be automatically processed.
d.remove_all_watchers()
d.set_watcher_loop_enabled(True)
d.register_click_target_selector_watcher("ClickAcceptWhenShowAggrement", [Selector(textContains="用户协议")],
Selector(textContains="同意", clickable=True))
d.register_press_key_watcher("PressBackWhenHomePageShows", [Selector(textContains="个人中心")], Keys.KEY_HOME)
d.register_none_op_watcher("RecordElementAppearTimes", [Selector(textContains="好的")])
d.set_watcher_enabled("ClickAcceptWhenShowAggrement", True)
d.set_watcher_enabled("PressBackWhenHomePageShows", True)
d.set_watcher_enabled("RecordElementAppearTimes", True)