Preface
Recently, I watched a video about how to use python to automate the app. I felt that the above was pretty good, so I used the knowledge I learned to make a program to automatically reply to QQ messages.
1. Preparation
1.1 Install the client module
Open the command window and enter the command:pip install appium-python-client
1.2 Install Appium Server
The download URL is:http://appium.io
1.3 Install JDK
After installation, add the environment variable JAVA_HOME, specify the jdk installation directory, the editor is:
1.4 Install android sdk
Add an environment variable ANDROID_HOME, set the value to the decompression directory of the sdk package, the editor is:
In addition, it is best to add the environment variable of adb.exe in the path
1.5 Projection software
The editor uses Mizhuo Same Screen Assistant, and the download URL is:https://cn.mirroid.com
2. Connect the phone
The USB connection used by the editor requires a data cable to connect the phone to the computer, and the developer mode of the phone needs to be turned on.
So how to open the developer mode, take Xiaobian’s mobile phone as an example (OPPO mobile phone) to the mobile phone settings, click About phone, and then keep clicking the version number, when the following words appear.
Then enter other settings (different phones may be different), you can see that there is one more developer option.
Enter the developer option, and then turn it on, you can find that there is a USB debugging switch here, turn it on.
After opening it, you can see that the screen of the mobile phone appears on this projection software.
3. Test whether the connection is successful
Enter in the command window: adb devices -l, if the following screen appears, it should be a successful connection!
Sometimes, an error may be reported. The general reason is that the adb.exe version of the projection software and the adb.exe version of the sdk are inconsistent. (I guessed by myself) you should only need to copy any adb.exe to the other That's it there.
4. Code Implementation
The reference code is as follows:
from appium import webdriver
import time
desired_caps={
'platformName':'Android',
'platformVersion':'8.1',
'deviceName':'xxx',
'appPackage':'com.tencent.qqlite', # 自动化应用
'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('当前QQ消息为%d个'%(len(list2)))
time.sleep(2)
list2[0].click()
def send_Message(text2:str): # 发消息
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("发送消息:%s"%(text2))
list4=[
"刘邦,字季,沛郡丰邑(今江苏省丰县)人。中国历史上杰出的政治家、战略家和军事指挥家,汉朝开国皇帝,汉民族和汉文化的伟大奠基者和开拓者,对汉族的发展以及中国的统一有突出贡献。",
"还没",
"湖南省,简称“湘”,是中华人民共和国省级行政区,省会长沙,界于北纬24°38′~30°08′,东经108°47′~114°15′之间,东临江西,西接重庆、贵州,南毗广东、广西,北连湖北,总面积21.18万平方千米。"
]
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('收到消息:%s'%(text)) # 接收消息
time.sleep(5)
if(text=='你好,请帮我查阅一下刘邦的简介'):
send_Message(list4[0])
elif(text=="你吃中饭了没"):
send_Message(list4[1])
elif(text=="介绍一下湖南呗!"):
send_Message(list4[2])
except Exception as e:
pass
In the process of running the code, some stuff will be automatically installed on the phone, just agree to install.