@[TOC] Although I add a lot of friends on Alipay, I usually have a lot of energy to steal, but because I am too lazy, so far no tree has been planted, so I want to steal energy this thing automatically. Before, the code was used to simulate the way of clicking on a mobile phone to realize automatic “like” in moments of friends, but at that time, the operation process of Ant Forest was much more complicated than that of “like” in moments of friends, so automatic stealing of energy was not realized at that time. Appium and UIAutomator2 have been recommended by some people in the comments on my blog. Recently, I took some time to study them and found that using UIAutomator2 is much easier, and since ant Forest has changed its version, the operation process of continuous energy stealing is much easier, so I can automatically steal energy. It looks like this. The full video is here
Next, I will introduce the principle and implementation method. First of all, if you are allowed to collect energy manually, what is your process?
- Open Alipay
- Open up the ant forest
- Take your energy first
- Move on to the next person with energy
- Take the energy of ta
- Repeat 4 and 5 until there is no power to steal
Fortunately, all of this can be automated with Uiautomator2. UiAutomator is a Java library provided by Google for android automated testing. It can obtain any control property of any APP on the screen and perform any operation on it. Uiautomator2 is a Python interface encapsulation on top of Uiautomator. Simply put, Uiautomator2 can see what controls are on the current screen of the phone, what their coordinates are, and can simulate clicking.
Uiautomator2 installation and use
For details, please refer to the official Uiautomator2 document. The installation method is very simple, and the following command can be used.
pip install --upgrade --pre uiautomator2
Copy the code
There are a couple of pits here,Before connecting the mobile phone to the computer, you need to open the developer mode first, and open THE USB debugging, USB installationIn order to ensure that UIAutomator2 has enough permissions to operate your phone.
When you use UIAutomator2 for the first time, it installs the ATX app on your phone, which you can use wirelessly later.
The specific implementation
How to open Alipay and enter ant forest?
Uiautomator2 da pp_start tuning up the application can be directly through the application package name (” com. Eg. Android. AlipayGphone “), what? You don’t know the name of alipay’s package? You can install Uiautomators2 and use its command line to check the package name of Alipay. For details, you can refer to its documentation.
How do you get into the ant forest? Uiautomators2 provides the ability to directly click on a text on the screen,D (text=" ant forest ").click()
.Therefore, it is suggested that you put ant Forest on the alipay home page, so that you can directly locate ant forest after opening Alipay.
Collect energy
The easiest way to locate a specific energy is to scan all possible energy locations (like the red box above) and then click the Find Energy button to move on to the next person.
How to stop
How to stop is a big problem, because the logic above is to simulate clicking, and if I don’t stop at the right time, who knows what it will click for me. After a few observations, I found that when there was no energy theft, Alipay would jump to the following page, where I just need to check if the “Back to my forest” button exists.
The complete code
The complete code is short as follows:
import uiautomator2 as u2
import time
import random
# d = u2.connect(
d = u2.connect("192.168.0.108") # To connect wirelessly, the computer and the phone need to be on the same LAN and have been initialized in a wired way
# d.app_stop("com.eg.android.AlipayGphone")
print("Open Alipay")
d.app_start("com.eg.android.AlipayGphone")
time.sleep(2) ## Sleep for 2s until Alipay is fully activated
print("Open the ant forest and wait five seconds...")
d(text="Ant Forest").click()
time.sleep(5) ## My phone is stuck and it takes a few seconds to fully load after entering the ant forest
def collectEnergy(cnt) :
print("Start stealing power for the % D time!" % cnt)
# Start scanning and clicking areas that have the ability to appear
for x in range(150.1000.150) :for y in range(600.900.150):
d.long_click(x + random.randint(10.20), y + random.randint(10.20), 0.1)
time.sleep(0.01)
ifcnt ! =1:
d.click(536.1816)
cnt = 1
while True:
collectEnergy(cnt)
a = d.xpath("//*[@resource-id='J_tree_dialog_wrap']").get().bounds
d.click(1000, a[3] -80) # Find the coordinates of the power button
## If "Return to my forest" appears on the page, there is no power to steal, end
if d.xpath('//*[@text=" return to my forest "]').click_exists(timeout=2.0) :break
cnt += 1
print(End of "# # # # # #")
Da pp_stop # (" com. Eg. Android. AlipayGphone ") # exit alipay
Copy the code
conclusion
Comments aside, it’s really less than 30 lines of code, but it’s recommended to use it quietly, don’t let your friends know you’re open and block you.