Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize automatic operation of Android mobile phone with Python

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to use Python to automate the operation of Android mobile phones. I think it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it.

1. [must] install adb tools

Adb, whose full name is Android Debug Bridge, is a debugging tool for Android system.

Download and install ADB Installer v1.4.3, download link: after http://pan.webscraping.cn:8000/index.php/s/7kDAJUOmKEa1h5N installation is complete, launch a new cmd window and enter adb devices. If there is no error prompt, the installation is successful.

To install adb under Ubuntu, please refer to this article: http://bernaerts.dyndns.org/linux/74-ubuntu/354-ubuntu-xenial-android-adb-fastboot-qtadb)

2. [optional] install UI Automator Viewer aids

In order to use UI Automator Viewer as an auxiliary analysis tool, we need to install Android SDK first, as follows:

1. Download and install Java 8

two。 Download and install Google Android SDK

3. Start Android SDK Manager, select and install Android SDK Platform-tools.

4. Double-click uiautomatorviewer.bat, launch UI Automator Viewer, and click the second icon to get the device screenshot and related UI information, as shown in the following figure.

[protagonist] AndroidViewClient

AndroidViewClient is an automated testing framework for Android applications written in pure Python, which does not rely on other programs (such as monkeyrunner, jython). AndroidViewClient controls Android devices by calling adb commands at the bottom, so the installation of adb is introduced at the beginning of this article.

Before starting below, it is assumed that you have installed and configured the Python runtime environment, otherwise please install Python 2.7 (Note: AndroidViewClient is not compatible with Python3).

1. Install AndroidViewClient

Project home page: https://github.com/dtmilano/AndroidViewClient

Easy_install installation is recommended:

Easy_install-upgrade androidviewclient

Detailed installation instructions can be found here: https://github.com/dtmilano/AndroidViewClient/wiki#installation

PS: there are many dependent libraries, so you need to be patient with installation.

two。 Test whether the installation is successful

Download the https://github.com/dtmilano/AndroidViewClient/archive/master.zip package, extract it and change it to the examples directory, execute python check-import.py, and output OK if there is no problem.

3. Write an example

Implement such a function:

Click the screen Wechat icon to start Wechat, click the first contact / group, and send a time message.

The code is as follows:

View plain copy to clipboard print?

# coding: utf-8

# Click the screen Wechat icon to launch Wechat, click the first contact / group, and send a time message

Import sys

Import os

Import re

Import time

From com.dtmilano.android.viewclient import ViewClient

Def test ():

# connect to your phone

Device, serialno = ViewClient.connectToDeviceOrExit ()

Vc = ViewClient (device, serialno)

# Press the home key

Device.press ('KEYCODE_HOME')

Time.sleep (3)

# find the Wechat icon

Vc.dump ()

Weixin_button = vc.findViewWithTextOrRaise (u 'Wechat')

# Click the Wechat icon

Weixin_button.touch ()

Time.sleep (10)

# find the first contact / group

# you can use UI Automator Viewer to see that the resource-id for the first contact / group is "com.tencent.mm:id/auj"

Vc.dump ()

Group_button = vc.findViewByIdOrRaise ("com.tencent.mm:id/auj")

# Click to join the group

Group_button.touch ()

Time.sleep (5)

# find the input box and enter the current time

Vc.dump ()

Vc.findViewByIdOrRaise ("com.tencent.mm:id/aep") .setText ('Now: {}' .format (time.strftime ('% Y-%m-%d% HV% MV% S')

Time.sleep (3)

# Click the send button

Vc.dump ()

Vc.findViewWithTextOrRaise (u' send') .touch ()

If _ _ name__ = ='_ _ main__':

Test ()

4. Advanced usage: the use of Culebra

Culebra can help us automatically generate (record) AndroidViewClient scripts. It provides a GUI, when we operate the phone through GUI, we will automatically generate AndroidViewClient control script template, we can modify based on this template script to achieve more complex functions. For a detailed description of Culebra, please see its home page: https://github.com/dtmilano/AndroidViewClient/wiki/culebra

Let's do a test and record a script for the "Click Wechat chat text input box, and then enter a text test": start Culebra first (the culebra file is located in the tools directory of the AndroidViewClient package, and I changed the suffix to .py). The command is as follows:

View plain copy to clipboard print?

Python culebra.py-G-- scale 0.5

The following is a screenshot of the GUI of Culebra after the operation is completed:

The following is a script automatically recorded by Culebra:

View plain copy to clipboard print?

#! / usr/bin/env python

#-*-coding: utf-8-*-

Import reimport sys

Import os

From com.dtmilano.android.viewclient import ViewClient

TAG = 'CULEBRA'

_ s = 5

_ v ='--verbose' in sys.argv

Kwargs1 = {'ignoreversioncheck': False,' verbose': False, 'ignoresecuredevice': False}

Device, serialno = ViewClient.connectToDeviceOrExit (* * kwargs1)

Kwargs2 = {'forceviewserveruse': False,' useuiautomatorhelper': False, 'ignoreuiautomatorkilled': True,' autodump': False, 'debug': {},' startviewserver': True, 'compresseddump': True}

Vc = ViewClient (device, serialno, * * kwargs2)

# vc.dump (window='-1') # FIXME: seems not needed

Vc.dump (window=-1)

No_id1 = vc.findViewByIdOrRaise ("id/no_id/1")

No_id1 = vc.findViewWithContentDescriptionOrRaise (U' current page, chat with Family Happy full Moon (19))

Com_tencent_mm___id_ds5 = vc.findViewByIdOrRaise ("com.tencent.mm:id/ds5")

Com_tencent_mm___id_cgo = vc.findViewByIdOrRaise ("com.tencent.mm:id/cgo")

Com_tencent_mm___id_ts = vc.findViewByIdOrRaise ("com.tencent.mm:id/ts")

Com_tencent_mm___id_iz = vc.findViewByIdOrRaise ("com.tencent.mm:id/iz")

Com_tencent_mm___id_j0 = vc.findViewByIdOrRaise ("com.tencent.mm:id/j0")

Com_tencent_mm___id_j0 = vc.findViewWithContentDescriptionOrRaise (u' returns'')

Com_tencent_mm___id_j1 = vc.findViewByIdOrRaise ("com.tencent.mm:id/j1")

Com_tencent_mm___id_j1 = vc.findViewWithTextOrRaise (u 'home full moon (19)')

Com_tencent_mm___id_iw = vc.findViewByIdOrRaise ("com.tencent.mm:id/iw")

Com_tencent_mm___id_iw = vc.findViewWithContentDescriptionOrRaise (u' chat message'')

No_id9 = vc.findViewByIdOrRaise ("id/no_id/9")

No_id10 = vc.findViewByIdOrRaise ("id/no_id/10")

Com_tencent_mm___id_lz = vc.findViewByIdOrRaise ("com.tencent.mm:id/lz")

No_id12 = vc.findViewByIdOrRaise ("id/no_id/12")

#.

# omit multiple lines of similar code

#.

Com_tencent_mm___id_aen = vc.findViewWithContentDescriptionOrRaise (u' switch to hold down and speak'')

Com_tencent_mm___id_aeo = vc.findViewByIdOrRaise ("com.tencent.mm:id/aeo")

Com_tencent_mm___id_aep = vc.findViewByIdOrRaise ("com.tencent.mm:id/aep")

Com_tencent_mm___id_aer = vc.findViewByIdOrRaise ("com.tencent.mm:id/aer")

Com_tencent_mm___id_aer = vc.findViewWithContentDescriptionOrRaise (u 'expression'')

Com_tencent_mm___id_aeu = vc.findViewByIdOrRaise ("com.tencent.mm:id/aeu")

Com_tencent_mm___id_aeu = vc.findViewWithContentDescriptionOrRaise (u' more function buttons, collapsed'')

Vc.findViewByIdOrRaise ("com.tencent.mm:id/aep") .setText (u "test")

Vc.sleep (_ s)

Vc.dump (window=-1)

You can repeat the above automatically by executing the script in the same window environment.

The above is how to use Python to automate the operation of Android mobile phones. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report