How to use Python to control mobile phone of II

  • 2021-12-13 08:30:34
  • OfStack

Catalog 1. Prologue 2. Front Work 3. Open APP 4. Get the package name of APP 5. Open specific page 6. Other details 7. Summary

1. Preface

Every year, when Taobao Double 101, it is always necessary to brush all kinds of browsing pages to collect energy or meow coins or something.

In that case, I always wonder if I can brush the web page through Python automatic call.

2. Front Work

This article is based on using Python to control the mobile phone (1). By default, ADB has been installed and configured with environment variables, Python environment has been installed, and uiautomator2 and weditor packages have been installed in Python.

3. Turn on APP

When we use the uiautomator2 package to open an APP, we can do this by clicking on a special position on the screen. However, the problem is that the program can't run because of the movement of our APP icon. The robustness and versatility are not high.

In fact, in the package uiautomator2, there is a way to open a specific APP by the package name of APP, such as opening and closing Taobao.


import uiautomator2 as u2
import time
d = u2.connect()  #  Connection equipment 
d.app_start("com.taobao.taobao")  #  Open Taobao 
time.sleep(10)  #  Wait 10 Seconds 
d.app_stop("com.taobao.taobao")  #  Close Taobao 

Another example is opening and closing WeChat:


import uiautomator2 as u2
import time
d = u2.connect()  #  Connection equipment 
d.app_start("com.tencent.mm")  #  Open WeChat 
time.sleep(10)  #  Wait 10 Seconds 
d.app_stop("com.tencent.mm")  #  Close WeChat 

4. Get the package name of APP

Sometimes, we don't know the package name of an APP. At this time, we can get the package name of APP by printing the current information of the device. First, we need to open the APP to be acquired and keep it at the front desk of the phone

Execute code:


import uiautomator2 as u2
import time
d = u2.connect()  #  Connection equipment 
print(d.info)  #  Print device information 

The output is as follows:

{'currentPackageName': 'com.taobao.taobao', 'displayHeight': 2111, 'displayRotation': 0, 'displaySizeDpX': 393, 'displaySizeDpY': 851, 'displayWidth': 1080, 'productName': 'cannon', 'screenOn': True, 'sdkInt': 29, 'naturalOrientation': True}
Process finished with exit code 0

In the printed Json key-value pair, the value corresponding to the key currentPackageName is the package name of APP at the front desk at this time, and the front APP is Taobao when the above results are operated.

5. Open a specific page

1 Generally speaking, if the page switch button contains specific text, it is the most convenient and accurate for us to locate directly through the text, such as opening the WeChat circle of friends:


import uiautomator2 as u2
import time
d = u2.connect()  #  Connection equipment 
d.app_start("com.tencent.mm")  #  Open WeChat 
time.sleep(2)  #  Wait 2 Seconds 
d(text=' Discover ').click()  #  Click on the control with the text "Discover" 
time.sleep(2)  #  Wait 2 Seconds 
d(text=' Friends circle ').click()  #  Click on the control with the text "Friends Circle" 

Because of the possible loading time of APP and the response time to click operation, try to leave enough response time for APP and mobile phone after each click operation. It is worth noting that if a common contact happens to have a nickname called "Discovery" after opening WeChat, it may be delayed. In this case, we need to use other positioning methods to locate specific controls.

If the text to be clicked is fixed, you can use d (text= "XXX") to select the control element, where XXX is the specific text. If part of the text is fixed, such as "I am the 11932nd visitor" for the first time and "I am the 12111th visitor" for the second time, we can click on this control through d (textContains= "I am the 1st"). click () or d (textContains= "Visitor"). click (), which can locate specific element controls through substrings.

Or use an example of entering a circle of friends:


import uiautomator2 as u2
import time
d = u2.connect()  #  Connection equipment 
d.app_start("com.tencent.mm")  #  Open WeChat 
time.sleep(2)  #  Wait 2 Seconds 
#  Click "Discover", 3 Choose 1
d(text=' Discover ').click()  #  Click on the text "Discover" control 
d(textContains=' Hair ').click()  #  Click on the control with "send" 
#  Pass WEditor Acquired xpath Positioning 
d.xpath('//*[@resource-id="com.tencent.mm:id/e8y"]/android.widget.LinearLayout[1]/android.widget.RelativeLayout[3]/android.widget.LinearLayout[1]').click()  
time.sleep(2)  #  Wait 2 Seconds 
#  Click on "Friends Circle", 3 Choose 1
d(text=' Friends circle ').click()  #  Click on the text as "Friends Circle" control 
d(textContains=' Friends ').click()  #  Click on the control with "friends" 
#  Pass WEditor Acquired xpath Positioning 
d.xpath('//*[@resource-id="android:id/list"]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]').click()

In fact, there are many different positioning methods, as long as you can navigate to only 1 specific controls and click. For example, when I was in Double 101, I switched to the click event on the page of collecting meow coins in Taobao:


import uiautomator2 as u2
import time
d = u2.connect()  #  Connection equipment 
d.app_start("com.taobao.taobao")  #  Open Taobao 
time.sleep(5)  #  Wait 5 Seconds 
d.xpath('//*[@content-desc=" Double 11 Super Meow Sugar "]').click()  #  Open the Meow Sugar page 

6. Other details

In the Double 101 Taobao activity, when you open the Meow Sugar page, you will pop up a pop-up box prompting whether to add this page to your collection, and click the button with the text "I'll think again". Note that to determine whether this control exists, clicking on a nonexistent control will report an error. If you are not sure if a control exists and don't want to judge, you need to wrap it with try...... catch......


if len(d(textContains=' I'll think again ')) > 0:  #  If this control exists 
    d(textContains=' I'll think again ').click()  #  Click "I'll think again" 

Click on the "Earn Sugar" control, because this control is often blocked by finger animation appearing on the screen, so you need to wait:


while len(d(textContains=' Earn sugar ')) <= 0:
    time.sleep(1)
d(textContains=' Earn sugar ').click()

Then click and wait, and then click the "Go Browse" button:


while len(d(textContains=' Go browsing ')) > 0:
    print(" Browse button detected ...")
    d(textContains=' Go browsing ').click()

Wait for 15 seconds (including the reaction time, it needs to wait 1 longer) and return:


d.press("back")  #  Equivalent to the return button of the mobile phone 

7. Summary

In fact, the specific part of the implementation is relatively simple, in this summary 1 under uiautomator2 other 1 functions.

About buttons:


import uiautomator2 as u2
import time
d = u2.connect()  #  Connection equipment 
d.app_start("com.tencent.mm")  #  Open WeChat 
time.sleep(10)  #  Wait 10 Seconds 
d.app_stop("com.tencent.mm")  #  Close WeChat 
0

About screen locking and unlocking:


import uiautomator2 as u2
import time
d = u2.connect()  #  Connection equipment 
d.app_start("com.tencent.mm")  #  Open WeChat 
time.sleep(10)  #  Wait 10 Seconds 
d.app_stop("com.tencent.mm")  #  Close WeChat 
1

About click and other operations (percentage of support):


import uiautomator2 as u2
import time
d = u2.connect()  #  Connection equipment 
d.app_start("com.tencent.mm")  #  Open WeChat 
time.sleep(10)  #  Wait 10 Seconds 
d.app_stop("com.tencent.mm")  #  Close WeChat 
2

Of course, there are other functions, such as sliding the screen up until the specified text appears:


d(scrollable=True).scroll.to(text="3 Grade 2 Class ")

This article is here, I hope to give you help, but also hope that you can pay more attention to this site more content!


Related articles: