preface
Recently, I looked at the video of app automation using python and thought it was good, so I made a program with what I learned to automatically reply to QQ messages.
- preparation
1.1 installing the client module
Open the command window and enter the command:
pip install appium-python-client
1.2 installing Appium Server
Download at: http://appium.io
1.3 installing JDK
After installation, add the environment variable JAVA_HOME, specify the installation directory of jdk, and the minor part is:
1.4 installing android sdk
Add an environment variable ANDROID_HOME, set the value to the decompression directory of the sdk package, and the small number is:
In addition, it's better to add ADB in the path Exe environment variable
1.5 screen projection software
Xiaobian uses the same screen assistant of mizhuo. The download website is: https://cn.mirroid.com
- Connect phone
The USB connection used by Xiaobian needs a data cable to connect the mobile phone with the computer. At the same time, it needs to turn on the developer mode of the mobile phone.
So how to open the developer mode? Take Xiaobian mobile phone as an example (OPPO mobile phone) to go to the mobile phone settings, click about mobile phone, and then click the version number all the time. When the following words appear.
Then enter other settings (different mobile phones may be different), and you can see that there is an additional developer option here.
Enter the developer option and turn it on. You can find a USB debugging switch here. Turn it on.
After you open it, you can see it on this projection software python basic tutorial The picture of the mobile phone appears.
- Test whether the connection is successful
In the command window, enter: adb devices -l, if the following appears: c# tutorial This picture should be successfully connected!
Sometimes, an error may be reported. The general reason is the ADB of the projection software Exe version and adb.exe version of sdk If the exe version is inconsistent, (Xiaobian guessed by himself), you should only add any ADB Just copy exe to another one.
- code implementation
Reference codes are as follows:
from appium import webdriver import time desired_caps={ 'platformName':'Android', 'platformVersion':'8.1', 'deviceName':'xxx', 'appPackage':'com.tencent.qqlite', # Automation application 'appActivity':'com.tencent.mobileqq.activity.SplashActivity', #'unicodeKeyboard':True, #'resetKeyboard':True, 'noReset':True, 'newCommandTimeout':6000, 'automationName':'UiAutomator2' } driver=webdriver.Remote('http://localhost:4723/wd/hub',desired_caps) driver.implicitly_wait(10) driver2=driver.find_element_by_id('recent_chat_list') list2=driver2.find_elements_by_class_name('android.widget.LinearLayout') print('current QQ Message is%d individual'%(len(list2))) time.sleep(2) list2[0].click() def send_Message(text2:str): # Send a message driver4=driver.find_element_by_id('inputBar') driver4.find_element_by_id('input').send_keys(text2) driver4.find_element_by_id('fun_btn').click() time.sleep(2) print("send message:%s"%(text2)) list4=[ "Liu Bang, Ziji, from Fengyi, Peijun county (now Fengxian County, Jiangsu Province). Outstanding statesmen, strategists and military commanders in Chinese history, the founding emperor of the Han Dynasty, and the great founders and pioneers of the Han nation and Han culture have made outstanding contributions to the development of the Han nationality and the reunification of China.", "not yet", "Hunan Province, abbreviated as "Xiang", is the provincial administrative region of the people's Republic of China and the provincial capital Changsha, which is bounded at 24 n°38′~30°08′,108 east longitude°47′~114°15′It is adjacent to Jiangxi in the East, Chongqing and Guizhou in the west, Guangdong and Guangxi in the South and Hubei in the north, with a total area of 21.18 10000 square kilometers." ] while True: try: driver3=driver.find_element_by_id('listView1') list3=driver3.find_elements_by_class_name('android.widget.RelativeLayout') text=list3[-1].find_element_by_id('chat_item_content_layout').text print('Received message:%s'%(text)) # receive messages time.sleep(5) if(text=='Hello, please check Liu Bang's profile for me'): send_Message(list4[0]) elif(text=="Have you had lunch yet"): send_Message(list4[1]) elif(text=="Introduce Hunan!"): send_Message(list4[2]) except Exception as e: pass