当前位置:网站首页>Python + appium automatic operation wechat is enough
Python + appium automatic operation wechat is enough
2020-11-06 01:17:55 【itread01】
## Introduction Appium Is an open source automated testing tool , Support Android、iOS Native applications on the platform , Support Java、Python、PHP And many languages . Appium It encapsulates Selenium, Can provide users with all the common JSON Format Selenium Commands and additional mobile device related control commands , such as : Multi touch gestures 、 Screen orientation, etc . ## The environment The main environment of this paper is as follows : + Win7 + JDK1.8 + Appium + Python3.7 + android-sdk + mumu Simulator ### JDK Download address :`https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html`, It can also be obtained directly at the end of the paper Configure environment variables : * Computers ( Right click )-> Properties -> Higher order system settings -> High order -> Environmental variables -> New environment variable `JAVA_HOME`, As shown in the figure :  * System variables -> find `Path` Variables -> Editor -> Add... At the end of the variable `;%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;` * newly build `CLASSPATH` Variables , The variable value is :`.;%JAVA_HOME%\lib;%JAVA_HOME%\lib\tools.jar` ### android-sdk Download address :`https://www.androiddevtools.cn/`, It can also be obtained directly at the end of the paper Configure environment variables : * And JDK The configuration is similar to , New environment variable `ANDROID_HOME`, The variable value is `android-sdk` Location , such as :`D:\android-sdk-windows` * stay `Path` Add... At the end of the variable `;%ANDROID_HOME%\tools;%ANDROID_HOME%\build-tools\30.0.0-preview;%ANDROID_HOME%\platform-tools` ### Appium Download address :`https://github.com/appium/appium-desktop/releases/tag/v1.18.3`, It can also be obtained directly at the end of the paper Install Python Ku :`pip install appium-python-client` Appium After the installation is completed and started , Click edit configuration , To configure JDK and android-sdk, As shown in the figure :  ### mumu Download address :`http://www.51xiazai.cn/soft/584481.htm`, It can also be obtained directly at the end of the paper mumu After the simulator is downloaded , In addition to changing the installation path according to your own needs , Other options can be installed by default , Open click after loading ` Application Center `, Search wechat , Find it and install it , After the installation of wechat, log in with your own wechat . Because we're through Android `adb` Connecting virtual machines , So it needs to be executed on the console `adb connect 127.0.0.1:7555` command , Let `adb` Connect to the virtual machine . After executing the above command of the connection Simulator , We can do it in `cmd` Console input `adb devices` View the currently connected virtual machine . ## Use Start... First Appium and mumu, Because we've already configured Appium, Now let's just click Appium Of ` Start the server ` Button , As shown in the figure below :  After startup, it is shown in the figure :  Now we can go through Python Let's start wechat , The code is implemented as follows : ``` desired_caps = { "platformName": "Android", # The operating system "deviceName": "emulator-5554", # Device ID "platformVersion": "6.0.1", # Device version number "appPackage": "com.tencent.mm", # app Package name "appActivity": "com.tencent.mm.ui.LauncherUI", # app When it starts, it's mainly Activity 'noReset': True, # Whether to keep session Information , You can avoid logging in again 'unicodeKeyboard': True, # Use unicodeKeyboard To send a string 'resetKeyboard': True # Hide the keyboard } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps) ``` After running the above code , If you find mumu Wechat in the simulator has been started , It means that the basic environment has been adjusted ; If you run the code and find that you can't call mumu Wechat in the simulator , First in `cmd` In order to implement `adb connect 127.0.0.1:7555` command , Run the program again . ### Add friends Let's use Appium Realize the operation of adding new friends , The basic process is : Open wechat -> Click on ⊕-> Choose new friends -> Enter the micro signal in the search box -> Click search -> Click Add to address book , The code implementation of the function is as follows : ``` desired_caps = { "platformName": "Android", # The operating system "deviceName": "emulator-5554", # Device ID "platformVersion": "6.0.1", # Device version number "appPackage": "com.tencent.mm", # app Package name "appActivity": "com.tencent.mm.ui.LauncherUI", # app When it starts, it's mainly Activity 'noReset': True, # Whether to keep session Information , You can avoid logging in again 'unicodeKeyboard': True, # Use unicodeKeyboard To send a string 'resetKeyboard': True # Hide the keyboard } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps) time.sleep(10) print(' Click on + No ') driver.find_element_by_id('com.tencent.mm:id/ef9').click() time.sleep(5) print(' Choose new friends ') driver.find_elements_by_id('com.tencent.mm:id/gam')[1].click() time.sleep(5) print(' Click on the search box ') driver.find_element_by_id('com.tencent.mm:id/fcn').click() time.sleep(5) print(' Enter the micro signal in the search box ') driver.find_element_by_id('com.tencent.mm:id/bhn').send_keys('ityard') time.sleep(3) print(' Click search ') driver.find_element_by_id('com.tencent.mm:id/ga1').click() time.sleep(3) print(' Click Add to address book ') driver.find_element_by_id('com.tencent.mm:id/g6f').click() ``` In a nutshell , In code, we use `driver.find_element_by_id('com.tencent.mm:id/xx')` To get the elements on wechat , If there's a repetition , You can use `driver.find_elements_by_id('com.tencent.mm:id/xx')[n]` Come and get , Through `send_keys('xx')` Realize the input of information , Through `click()` To realize the click operation . Above we said pass `find_element(s)_by_id('com.tencent.mm:id/xx')` To get the elements , So how to determine `xx` Well ? Now let's take a look at . First of all, let's click Appium Position of magnifying glass in , As shown in the figure below :  Click to enter the following interface :  We're in the picture ` The required function ` In the code below `desired_caps` Information configuration in , After configuration, click ` Start the session ` Button , When we start, we'll find Appium China and mumu The effect of wechat in is inconsistent , As shown in the figure below :  At this point, just click the rearrange button circled in the red box above , Now we can determine the value of the element ( That is to say `xx`), such as : Let's confirm the new location in wechat `⊕` Value , Click on... With the mouse `⊕` You can view , As shown in the figure below :  We then click on `⊕`, The operation steps are as follows : Come first mumu In the simulator, select the... In wechat `⊕`, As shown in the figure below :  Click and then go to Appium Click the refresh button , As shown in the figure below :  From the figure, we can see that the value of each option in the list is `com.tencent.mm:id/gam`, At this point in the code we use `driver.find_elements_by_id('com.tencent.mm:id/gam')[1]` Come and get it , Through the above introduction, I believe you have a good understanding of Appium We have a basic understanding of the use of . ### Send a message The basic process we simulate is : Open wechat -> Click on the magnifying glass of search -> Enter your friend's nickname in the search box -> Click on the friends you found -> Transmit text + expression , The code is implemented as follows : ``` desired_caps = { "platformName": "Android", # The operating system "deviceName": "emulator-5554", # Device ID "platformVersion": "6.0.1", # Device version number "appPackage": "com.tencent.mm", # app Package name "appActivity": "com.tencent.mm.ui.LauncherUI", # app When it starts, it's mainly Activity 'noReset': True, # Whether to keep session Information , You can avoid logging in again 'unicodeKeyboard': True, # Use unicodeKeyboard To send a string 'resetKeyboard': True # Hide the keyboard } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps) time.sleep(10) print(' Click on the wechat search box ') driver.find_element_by_id('com.tencent.mm:id/f8y').click() time.sleep(10) print(' Enter the search information in the search box ') driver.find_element_by_id('com.tencent.mm:id/bhn').send_keys('Python waiter ') time.sleep(3) print(' Click on the friends you found ') driver.find_element_by_id('com.tencent.mm:id/tm').click() time.sleep(3) # Input text driver.find_element_by_id('com.tencent.mm:id/al_').send_keys('hello') time.sleep(3) # Input the expression driver.find_element_by_id('com.tencent.mm:id/anz').click() time.sleep(3) driver.find_element_by_id('com.tencent.mm:id/rv').click() # Click the send button to send information driver.find_element_by_id('com.tencent.mm:id/anv').click() # sign out driver.quit() ``` And finally , Because the simulator may be slower , If the program runs with errors , You can change the waiting time between `time.sleep(x)` Set it bigger . Software and code in the public name **Python waiter ** Backstage reply **201104**
版权声明
本文为[itread01]所创,转载请带上原文链接,感谢
边栏推荐
- C++ 数字、string和char*的转换
- C++学习——centos7上部署C++开发环境
- C++学习——一步步学会写Makefile
- C++学习——临时对象的产生与优化
- C++学习——对象的引用的用法
- C++编程经验(6):使用C++风格的类型转换
- Won the CKA + CKS certificate with the highest gold content in kubernetes in 31 days!
- C + + number, string and char * conversion
- C + + Learning -- capacity() and resize() in C + +
- C + + Learning -- about code performance optimization
猜你喜欢
-
C + + programming experience (6): using C + + style type conversion
-
Latest party and government work report ppt - Park ppt
-
在线身份证号码提取生日工具
-
Online ID number extraction birthday tool
-
️野指针?悬空指针?️ 一文带你搞懂!
-
Field pointer? Dangling pointer? This article will help you understand!
-
HCNA Routing&Switching之GVRP
-
GVRP of hcna Routing & Switching
-
Seq2Seq实现闲聊机器人
-
【闲聊机器人】seq2seq模型的原理
随机推荐
- LeetCode 91. 解码方法
- Seq2seq implements chat robot
- [chat robot] principle of seq2seq model
- Leetcode 91. Decoding method
- HCNA Routing&Switching之GVRP
- GVRP of hcna Routing & Switching
- HDU7016 Random Walk 2
- [Code+#1]Yazid 的新生舞会
- CF1548C The Three Little Pigs
- HDU7033 Typing Contest
- HDU7016 Random Walk 2
- [code + 1] Yazid's freshman ball
- CF1548C The Three Little Pigs
- HDU7033 Typing Contest
- Qt Creator 自动补齐变慢的解决
- HALCON 20.11:如何处理标定助手品质问题
- HALCON 20.11:标定助手使用注意事项
- Solution of QT creator's automatic replenishment slowing down
- Halcon 20.11: how to deal with the quality problem of calibration assistant
- Halcon 20.11: precautions for use of calibration assistant
- “十大科学技术问题”揭晓!|青年科学家50²论坛
- "Top ten scientific and technological issues" announced| Young scientists 50 ² forum
- 求反转链表
- Reverse linked list
- js的数据类型
- JS data type
- 记一次文件读写遇到的bug
- Remember the bug encountered in reading and writing a file
- 单例模式
- Singleton mode
- 在这个 N 多编程语言争霸的世界,C++ 究竟还有没有未来?
- In this world of N programming languages, is there a future for C + +?
- es6模板字符
- js Promise
- js 数组方法 回顾
- ES6 template characters
- js Promise
- JS array method review
- 【Golang】️走进 Go 语言️ 第一课 Hello World
- [golang] go into go language lesson 1 Hello World