Click “AirPython” and select “Top public account”.

Get Python technology dry stuff for the first time!

It takes about 8 minutes to read the text.

The target scene

I believe everyone is busy in various wechat groups these two days, focusing on snatching red envelopes. Is it possible to use Python to automatically grab red packets?

The answer is yes. The purpose of this article is to use “Airtest” to achieve automatic snatching group red packets.

Ps: Red envelopes are just for fun. I hope this article is just for learning.

The preparatory work

First of all, you need to configure the Airtest environment on this machine, and check the “original link” for configuration and basic usage.

Second, since I use an Android phone, I need to configure the Android development environment in advance to ensure that the “ADB” command works properly.

Open the AirtestIDE, connect it to a PC with a data cable, and click the connect button in the upper right corner to connect to the device.

After the device is successfully connected, then slide the mobile phone interface to the page containing the “wechat icon”, you can officially enter the following operations.

Create a project using the AirtestIDE, select Android device type and generate an initialized code in the coding area.

# -*- encoding=utf8 -*-__author__ = "xingag"from airtest.core.api import *auto_setup(__file__)from poco.drivers.android.uiautomation import AndroidUiautomationPocopoco = AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False)Copy the code

Copy the code above into Pycharm and install the “Pocoui” library.

pip3 install pocouiCopy the code

Ps: Note that the Mac OS installation will prompt that ADB does not have permission to run, just grant permission to respond.

# chmod + x/Users/xingag/anaconda3 / lib/python3.7 / site - packages/airtest/core/MAC/android/static/the adb/adbchmod + x [native adb path]Copy the code

Here I created a new wechat group named “grab a red envelope” for the convenience of the subsequent test.

Analysis methods

First we need to open the wechat App.

In contrast to Appium, which requires a package name and the form of starting an Activity, Airtest opens the application with a single line of code.

Poco (text=' wechat ').click()Copy the code

The first Tab is the wechat message list, which shows all group chats, individual chats and some messages.

Find all message list elements by element name, and then you can see the contents of all messages.

Item_elements = poco(name=' com.0700. mm:id/b4m'). Offspring (' com.0700. mm:id/b4o')# Get names = of all messages in the list list(map(lambda x: x.get_text(), item_elements))Copy the code

Find the target group element, you can perform the click operation, enter the group chat.

Item_elements [index].click()Copy the code

It then gets all the message elements of the current group chat.

Msg_list_elements_pre = poco(" Android.widget.listview ").children()Copy the code

Grab red envelopes need to start from a recent red envelope grab, all need to reverse the group chat message.

msg_list_elements = []for item in msg_list_elements_pre:    msg_list_elements.insert(0, item)Copy the code

Then you can traverse the message list and get the “wechat red envelope” wechat red envelope logo and has not grabbed the red envelope “contains their own” message elements.

for msg_element in msg_list_elements: Offspring ('com.tencent. Mm :id/ APF ') # 2.2 Whether the element has_click_element = Msg_element. Offspring (' com.tceh.mm :id/ape') # 2.3 offspring Print (' find a red packet ') if has_click_element.exists() and (has_click_element.get_text() == 'received' or Has_click_element.get_text () == 'select * from '): print(' select * from ') else: print(' select * from ') Print (' Red envelope element not present ') continueCopy the code

The operation of grabbing red envelopes is very simple. Click the red envelope that has not been grabbed, to grab the red envelope dialog interface, click the “grab” button, you can simulate the operation of grabbing a red envelope.

After grabbing the red envelope, you need to simulate clicking the back button on the phone to the message list page, and then perform the next operation to grab the red envelope.

msg_element.click()click_element = poco("com.tencent.mm:id/cv0")if click_element.exists(): Click_element.click () # return keyevent('BACK')Copy the code

Finally, define the above red envelope snatching operation as a method that loops through the message list and executes the “red envelope snatching” operation as soon as it gets a new red envelope.

While True: get_red_package() print(' Sleep for 1 second, continue to refresh the page, start to grab red packets. ') sleep(1)Copy the code

The results summary

When the program is executed, the phone will automatically open wechat, find the corresponding group chat, enter the message list page, “regularly” check the information in the list, and if there is a red envelope found, start grabbing the red envelope.

Of course, the message list page can be swiped up by the swipe function to retrieve more red packets in a similar way.

I have uploaded all the source code to the back platform, public number reply “grab red envelope” can be obtained.

If you think the article is good, please like it and share it. Your affirmation is my biggest encouragement and support.