Article | so-and-so rice \
Source: Python Technology “ID: pythonall”
The Spring Festival is coming, the red envelopes are likely to be rampant in the wechat groups, whether we are all the same do not grab the red envelopes they feel that they missed a hundred million, but will always be delayed by this matter and regret to miss, the following use Python to write an automatic grab red envelopes code \
Start the entrance
The configuration of the startup program is the same as the configuration of the public account article “using Python + Appium method to automatically clean up wechat zombie friends”
from appium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.support import expected_conditions as EC
desired_capabilities = {
'platformName': 'Android', # operating system'deviceName': '2a254a02', # device ID'platformVersion': '10.0.10', # Device version number, check in the phone Settings'appPackage': 'com.tencent.mm', # app package name'appActivity': 'com.tencent.mm.ui.LauncherUI', # app starts the main Activity'noReset'} driver = webdriver.remote ('http://localhost:4723/wd/hub', desired_capabilities)
# 设置等待超时时间
wait = WebDriverWait(driver, 60)
Copy the code
Click to enter the chat window
In wechat, the latest chat records are usually placed first, so you only need to open the first chat window to check whether there is a red envelope. You can find all the chat information with id com.tencent. Mm :id/e3x, and we take the index of the first chat group
Red_packet_group = driver.find_elements_by_id('com.tencent.mm:id/e3x') [0]
red_packet_group.click()
Copy the code
Find a red envelope
After entering the chat group, check the red envelope picture to see if there is a red envelope. Its ID is com.tencent. Mm :id/ R2
Reds = driver. Find_elements_by_id ('com.tencent.mm:id/r2')
if len(reds) == 0:
driver.keyevent(4)
Copy the code
Grab a red envelope
After clicking the red envelope, the following three things will happen
- The red envelope has been received by himself
- The red envelope was too slow to get it
- Red envelope not received
In the first two cases, the red envelope has expired, and in the last case, the red envelope can be opened
The red envelope has expired
In the above code is to use THE ID to check whether the element exists, here use to find the text has been deposited in change and slow to determine whether the red envelope has expired
Def is_element_exist_by_xpath(driver, text): try: driver.find_element_by_xpath(text) except Exception as e:return False
else:
returnIs_open = is_element_exist_by_xpath(driver,'/ / android. Widget. TextView [contains (@ text, "has been deposited in the change")]'Upon hearing = is_element_exist_by_xpath(driver,'/ / android. Widget. TextView [contains (@ text, "both the")]')
if is_open or is_grabbed:
driver.keyevent(4)
Copy the code
Open the red envelopes
Opening the red envelope is relatively easy, just need to find the id of the opening character
wait.until(EC.element_to_be_clickable((By.ID, "com.tencent.mm:id/den"))).click()
wait.until(EC.element_to_be_clickable((By.ID, "com.tencent.mm:id/dm"))).click()
Copy the code
To delete a red envelope
Finally, we will delete the red envelope to prevent the red envelope from being opened repeatedly. When long pressing the red envelope, wechat red envelope will appear delete button
TouchAction(driver).long_press(red).perform()
wait.until(EC.element_to_be_clickable((By.ID, "com.tencent.mm:id/gam"))).click()
wait.until(EC.element_to_be_clickable((By.ID, "com.tencent.mm:id/doz"))).click()
Copy the code
conclusion
This is the third article about learning and using Appium. Appium can automate the operation of mobile phones.
PS: Reply “Python” within the public number to enter the Python novice learning exchange group, together with the 100-day plan!
Old rules, brothers still remember, the lower right corner of the “watching” click, if you feel the content of the article is good, remember to share moments to let more people know!
[Code access ****]