Although I pay treasure to add a lot of friends, usually have a lot of energy can “steal”, but because I am too lazy, so far did not have many trees, so I want to steal energy this matter of automation. Before using the code to simulate the way of mobile phone click, the circle of friends automatically like. But at the time, ant Forest was much more complicated than “like”, so there was no automatic energy stealing.

Later, many netizens recommended appium and UIAutomator2 to me in the group and comment section. Recently, I took some time to study it and found that it is much easier to use UIAutomator2, and the operation process of continuous energy stealing is also much simpler due to the revamp of Ant Forest. So I’m able to automatically steal power and see the full video below.

(www.ixigua.com/692612)… .

Next, I will introduce the principle and implementation method. First of all, if you are allowed to collect energy manually, what is your process?

  1. Open Alipay;
  2. Open the ant forest;
  3. Take your energy first;
  4. Jump to the next person with energy;
  5. Take the energy of ta
  6. Repeat 4 and 5 until there is no energy 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 speaking, Uiautomator2 can see what controls are on the current screen of the phone, their coordinates, 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

Before connecting the phone to the computer, you need to open the developer mode first, and start USB debugging and USB installation (as shown below), so as 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 a direct click on a text location 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") # Through wireless connection, the computer and the phone need to be on the same LAN, And need to be done in the cable way to initialize the da pp_stop # (" com. Eg. Android. AlipayGphone ") print da pp_start (" open pay treasure ") (" com. Eg. Android. AlipayGphone ") Print (" Open ant forest, wait 5 seconds... ") ) d(text=" ant forest ").click() time.sleep(5) ## Def collectEnergy(CNT): print(" start stealing energy %d ") % CNT) # 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) if CNT! C lick(536,1816) CNT = 1 while True: collectEnergy(cnt) a = d.xpath("//*[@resource-id='J_tree_dialog_wrap']").get().bounds d.click(1000, If d.xpath('//*[@text=" return to my forest "]').click_exists(timeout=2.0): Break CNT + = 1 print (" # # # # # # ") da pp_stop # (" com. Eg. Android. AlipayGphone ") # # exit pay treasure to spare grass, public numberCopy 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.