Automated Testing of WeChat Mini Programs Using Appium and Python WebView

Setting Up X5 Debugging Environment

To enable debugging capabilities, access these URLs:

http://debugmm.qq.com/?forcex5=true
http://debugx5.qq.com
http://debugtbs.qq.com

The first two URLs typically suffice for most cases.

In WeChat, navigate to: http://debugx5.qq.com, then follow the interface instructions and enable the required options.

Chrome Device Inspection Setup

Access: chrome://inspect/devices#devices in Google Chrome browser.

You may encounter this error message:

"Pending authentication: please accept debugging session on the device"

Resolution Steps:

Disable and re-enable developer mode on your mobile device.

Handling 404 and Blank Page Issues

When clicking the "inspect" button, you might experience 404 errors or blank pages. Solutions include using proxy services or custom WebView packages available through specialized vendors.

Alternative Method: Mini Program Debug Tool

An additional approach involves the debug tool appearing in the bottom-right corner of the interface, which enables element identification within the mini program.

Identifying Mini Program Process Information

Execute these ADB commands to locate process information:

adp shell dumpsys activity top | grep ACTIVITY
adp shell ps 4527

The process com.tencent.mm:appbrand0 is essential for proper operation.

Implementation Code Example

from appium import webdriver
from time import sleep
driver_options = {
    'platformName': 'Android',
    'fastReset': 'false',
    'noReset': True,
    'platformVersion': '9',
    'deviceName': 'b938d4a4',
    'appPackage': 'com.tencent.mm',
    'appActivity': '.ui.LauncherUI',
    'fullReset': 'false',
    'chromeOptions': {
        'androidProcess': 'com.tencent.mm:appbrand0'
    }
}
automation_driver = webdriver.Remote('http://0.0.0.0:4723/wd/hub', driver_options)
sleep(5)
automation_driver.find_element_by_android_uiautomator('text("微信")').click()

# Screen scrolling function
def scroll_down(duration):
    screen_width = automation_driver.get_window_size()['width']
    screen_height = automation_driver.get_window_size()['height']
    start_x = int(screen_width * 0.5)
    start_y = int(screen_height * 0.25)
    end_y = int(screen_height * (0.25 + duration))
    automation_driver.swipe(start_x, start_y, start_x, end_y, 500)

scroll_down(0.4)
sleep(2)
automation_driver.find_element_by_android_uiautomator('text("xxx")').click()
sleep(5)
print(automation_driver.contexts)
automation_driver.switch_to.context('WEBVIEW_com.tencent.mm:tools')
sleep(5)
automation_driver.find_element_by_css_selector('.footer2').click()

Resolving ChromeDriver Version Conflicts

If encountering version mismatch errors (e.g., ChromeDriver 2.45 corresponds to Chrome 70), visit:

https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/web/chromedriver.md

to find compatible versions. For Chrome version 66.0.3359.126, download ChromeDriver 2.39.

Locate the ChromeDriver path in Appium logs and replace the existing file.

Window Handle Management Solution

When element location fails, implement window handle management:

window_handles = automation_driver.window_handles
print(window_handles)
print(automation_driver.current_window_handle)
automation_driver.switch_to_window(window_handles[1])

Complete Working Implementation

from appium import webdriver
from time import sleep
driver_options = {
    'platformName': 'Android',
    'fastReset': 'false',
    'noReset': True,
    'platformVersion': '9',
    'deviceName': 'b938d4a4',
    'appPackage': 'com.tencent.mm',
    'appActivity': '.ui.LauncherUI',
    'fullReset': 'false',
    'chromeOptions': {
        'androidProcess': 'com.tencent.mm:appbrand0'
    }
}
automation_driver = webdriver.Remote('http://0.0.0.0:4723/wd/hub', driver_options)
sleep(5)
automation_driver.find_element_by_android_uiautomator('text("微信")').click()

# Screen scrolling function
def scroll_down(duration):
    screen_width = automation_driver.get_window_size()['width']
    screen_height = automation_driver.get_window_size()['height']
    start_x = int(screen_width * 0.5)
    start_y = int(screen_height * 0.25)
    end_y = int(screen_height * (0.25 + duration))
    automation_driver.swipe(start_x, start_y, start_x, end_y, 500)

scroll_down(0.4)
sleep(2)
automation_driver.find_element_by_android_uiautomator('text("xxx")').click()
sleep(5)
print(automation_driver.contexts)
automation_driver.switch_to.context('WEBVIEW_com.tencent.mm:tools')
sleep(5)
window_handles = automation_driver.window_handles
print(window_handles)
print(automation_driver.current_window_handle)
automation_driver.switch_to_window(window_handles[1])
automation_driver.find_element_by_css_selector('.footer2').click()

The implementation now functions successfully.

Tags: appium python wechat-mini-programs webview-automation mobile-testing

Posted on Mon, 13 Jul 2026 16:54:43 +0000 by gabo0303