Interface Monitor

The monitor is used to listen to interface changes in real-time and perform preset actions when predefined conditions are met, such as clicking elements, pressing keys, or recording occurrence counts. You can understand it as the underlying implementation of functions like LiTiaotiao or similar automatic ad-skipping features. Since this function will automatically perform click or keypress operations, unexpected actions may occur when manual intervention is needed, so please use it with caution.

Tip

Normally, monitors and event registrations should be done at the beginning of the script and generally won’t be done during execution.

Enable Monitor

The following interface will enable the interface monitor. By default, the monitor is disabled.

Attention

Any matching event operations that occur on the interface before enabling the monitor or enabling monitor events will not be processed!

d.set_watcher_loop_enabled(True)

Check if Monitor is Enabled

You can use the following interface to check whether the current interface monitor is enabled.

d.get_watcher_loop_enabled()

Disable Monitor

Disable the monitor loop, which will no longer automatically process registered events such as clicks.

d.set_watcher_loop_enabled(False)

Attention

Registering too many monitoring events may affect system performance or operation real-time performance.

Remove All Monitors

Remove all previously applied monitoring events, which can clear rules applied by previous or abnormally interrupted scripts. It is recommended to execute this before starting each script to prevent events registered by previous tasks from affecting the normal processing flow of the current stage.

d.remove_all_watchers()

Register Click Events

Click events will automatically perform click operations when matching selectors appear on the interface, such as automatically bypassing software pop-ups like terms of service, update prompts, etc., or automatic clicking on certain specific interfaces.

Attention

Any matching event operations that occur on the interface before enabling the monitor or enabling monitor events will not be processed!

d.register_click_target_selector_watcher("ClickAcceptWhenShowAggrement", [Selector(textContains="User Agreement")], Selector(textContains="Agree", clickable=True))

Hint

The second parameter supports multiple Selectors, meaning that all Selector conditions must be met for the event to be processed.

Registering the above event will automatically click the “Agree” button when an interface containing the text “User Agreement” appears.

Register Key Events

Key events will automatically perform key operations when matching selectors appear on the interface, such as automatic returns, which can automatically avoid entering certain interfaces. Of course, the same operation can also be achieved using click events.

Attention

Any matching event operations that occur on the interface before enabling the monitor or enabling monitor events will not be processed!

d.register_press_key_watcher("PressBackWhenHomePageShows", [Selector(textContains="Personal Center")], Keys.KEY_HOME)

Hint

The second parameter supports multiple Selectors, meaning that all Selector conditions must be met for the event to be processed.

Registering the above event will automatically press the phone’s HOME key when an interface containing the text “Personal Center” appears.

Register Count Events

Count events increment a counter by 1 when certain matching selectors appear on the interface. When important information appears fleetingly on the interface, you can use this function to count whether certain interfaces have appeared or how many times they have appeared.

Attention

Any matching event operations that occur on the interface before enabling the monitor or enabling monitor events will not be processed!

d.register_none_op_watcher("RecordElementAppearTimes", [Selector(textContains="OK")])

Hint

The second parameter supports multiple Selectors, meaning that all Selector conditions must be met for the event to be processed.

The above event records the number of times an interface containing “OK” appears. Use the following interface to get the trigger count of the count event.

d.get_watcher_triggered_count("RecordElementAppearTimes")

Enable Events

After events are registered, they need to be enabled to be registered to the interface monitor loop, otherwise they will not be applied.

Attention

Any matching event operations that occur on the interface before enabling the monitor or enabling monitor events will not be processed!

d.set_watcher_enabled("RecordElementAppearTimes", True)

Disable Events

You can call the following interface to make the interface monitor stop monitoring a certain event.

Attention

Any matching event operations that occur on the interface before enabling the monitor or enabling monitor events will not be processed!

d.set_watcher_enabled("RecordElementAppearTimes", False)

Remove Events

Completely remove an event from the monitor, and it will also be deleted from registered events.

d.remove_watcher(name)

Check if Event is Enabled

Call the following interface to check if an event is properly enabled, to prevent situations where events are registered but not enabled.

d.get_watcher_enabled(name)

Complete Example Code

Below is an example code block that enables the monitor loop and registers three monitor events. When text containing “User Agreement” appears on the interface, it automatically clicks agree. When text containing “Personal Center” appears on the interface, it immediately returns to the desktop. When text containing “OK” appears on the interface, it records the number of times this interface appears.

Attention

Any matching event operations that occur on the interface before enabling the monitor or enabling monitor events will not be processed!

d.remove_all_watchers()
d.set_watcher_loop_enabled(True)

d.register_click_target_selector_watcher("ClickAcceptWhenShowAggrement", [Selector(textContains="User Agreement")],
                                         Selector(textContains="Agree", clickable=True))
d.register_press_key_watcher("PressBackWhenHomePageShows", [Selector(textContains="Personal Center")], Keys.KEY_HOME)
d.register_none_op_watcher("RecordElementAppearTimes", [Selector(textContains="OK")])

d.set_watcher_enabled("ClickAcceptWhenShowAggrement", True)
d.set_watcher_enabled("PressBackWhenHomePageShows", True)
d.set_watcher_enabled("RecordElementAppearTimes", True)