1. An overview of the
I just got two messages, one good and one bad.
First the good news, the good news is that someone in the wechat group to send red envelopes, happy ~
But on second thought, a few times before a red envelope did not grab, this time?? An involuntary sigh…
After a while, the inner mood gradually calmed down.
I thought to myself, “Do you just give up? I had instant noodles for dinner (instant noodles felt offended
)? But manual snatching is definitely out of the question, after all, hands can do nothing! Then we can only try to achieve automatic red envelope snatching through programming!”
Now to clarify the idea, the basic situation of wechat group red envelope: each red envelope will have some time interval with the last one, the basic idea of automatic red envelope snatching is as follows:
-
Manually empty the red envelope records in the previous wechat group
-
Execute the automatic red envelope snatching procedure, enter the wechat group that sends the red envelope (you can temporarily put it on the top), detect whether there is a red envelope in the group, and click the red envelope when it is found
-
Detect whether the red envelope has been claimed (judge whether the red envelope has an open character). If the red envelope has not been claimed, click the open character to claim the red envelope, and then return to the group chat interface to delete the record of the red envelope that has been claimed; If the red envelope has been claimed, go back to the group chat interface to delete the record of the red envelope that has been claimed, and so on
2. Environmental
The main environment of this paper is as follows:
-
Win7
-
Millet 5 s
-
Python3.7
-
Appium1.5
-
WeChat 7.0.20
3. The implementation
Now let’s start to manually type the code. Let’s take a look at the implementation.
First look at the configuration information, the code implementation is as follows:
Desired_caps = {"platformName": "Android", # system "platformVersion": "8.0.0", # system "deviceName": "M5s ", # device name "appPackage": "com.tencent. Mm ", # package name "appActivity": ".ui.LauncherUI", # app startup main Activity 'unicodeKeyboard': True, # use the built-in input method 'noReset': True # Retain session information to avoid re-login}Copy the code
After clicking the red envelope, we need to judge whether the red envelope is received, that is, whether there is an open character, as shown in the picture:
So we define a method to determine whether the element exists, the code implementation is as follows:
Def is_element_exist(driver, by, value): try: driver. Find_element (by=by, value=value) except Exception as e: return False else: return TrueCopy the code
Because red envelopes, whether received by themselves or others, will be deleted after the red envelope record, so we define a method to delete the red envelope, the code implementation is as follows:
Def del_red_envelope(wait, driver): R8 = wait. Until (ec.element_to_be_clickable ((by.id, "Com.0700.mm :id/r8"))) TouchAction(driver).long_press(r8).perform() # click long press to display delete wait.until(EC.element_to_be_clickable((By.ID, Wait. Until (ec.element_to_be_clickable ((by.id, "com.tencent.mm:id/doz"))).click()Copy the code
After long press to get the red envelope effect picture is as follows:
Click and long press to display the effect picture of the deleted item as follows:
We then look at the main program implementation into the red envelope group, the code is as follows:
while True: Wait. Until (ec.element_to_be_clickable ((by.id, Is_open = is_element_exist(driver, "id",)).click() print(" iD ") "com.tencent.mm:id/den"); Print (" is_open ", is_open) if is_open == True # Red envelope not received, Wait. Until (ec.element_to_be_clickable ((by.id, Wait. Until (ec.element_to_be_clickable ((by.id, Del_red_envelope (wait, driver) else: del_red_envelope(wait, driver) Del_red_envelope (wait, driver) del_red_envelope(wait, driver)Copy the code
This is the end of spicy
PS: If you need Python learning materials, please click on the link below to obtain them
Click to join the complete code and click on it