Automate WeChat Unread Messages on Android with UiAutomator2 and WEditor

This tutorial shows how to use UiAutomator2 and WEditor to create a Python script that automatically clears unread notifications in the WeChat Android app by connecting to the device, locating UI elements, and performing fast double‑click actions until no messages remain.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Automate WeChat Unread Messages on Android with UiAutomator2 and WEditor

Scenario

A user is frustrated by the many red notification dots in WeChat, which require manual clicking and cause anxiety. The article proposes using UiAutomator2 to write an automation script that clears these notifications.

WeChat UI with red notification dots
WeChat UI with red notification dots

Preparation

Before starting, set up the Android development environment on a PC and install the required Python libraries and applications.

# Install dependencies
pip3 install -U uiautomator2

# Install pillow if screenshot capability is needed
pip3 install pillow

# Install weditor for real‑time UI inspection
pip3 install -U weditor

Install the atx‑agent service on the phone:

# Install the APK service on the device
python -m uiautomator2 init

Practical Example

Run the weditor command to open the device management page in a browser, note the device IP address, and connect to the device.

import uiautomator2 as u2

PACKAGE_NAME = 'com.tencent.mm'  # WeChat package name
device = u2.connect('192.168.0.101')
width, height = device.window_size()
device.app_start(PACKAGE_NAME, stop=True)

Wait for the home page to load completely:

def __wait_home_appear(self):
    """Wait until the home page is fully loaded."""
    self.device(resourceId='com.tencent.mm:id/cns', text='微信').wait(timeout=20)
    self.device(resourceId='com.tencent.mm:id/cns', text='通讯录').wait(timeout=20)
    self.device(resourceId='com.tencent.mm:id/cns', text='发现').wait(timeout=20)
    print('Home page loaded')

Check whether there are unread messages:

def __has_unread_msg(self):
    """Return True if there is any unread message."""
    try:
        number_unread_msg = self.device(resourceId='com.tencent.mm:id/gik')
        return number_unread_msg.get_text() != ''
    except Exception:
        return False

Locate an unread message element and click it if it exists:

# Unread message element
element = self.device(resourceId='com.tencent.mm:id/ga3', instance=0)

if element.exists and self.__is_number(element.get_text()):
    element.click()
    if not self.__is_home_page():
        self.device.press('back')
else:
    pass

Fast double‑click the bottom Tab to jump directly to the unread item:

def click_twice_quickly(device, element):
    """Perform a quick double click on the element."""
    bounds = element.bounds()
    center_x = (bounds[2] + bounds[0]) / 2
    center_y = (bounds[3] + bounds[1]) / 2
    device.double_click(center_x, center_y, 0.05)

Loop until no unread messages remain:

while True:
    if self.__has_unread_msg():
        click_twice_quickly(self.device, element)
        # Process the unread message here
        pass
    else:
        logger.debug('No more unread messages, exiting')
        break

Conclusion

The script clears all unread messages on the WeChat home page; the same technique can be applied to handle notifications in contacts, Moments, or personal chats.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonAndroidWeChatWEditoruiautomator2
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.