Python + appium automatic operation wechat is enough

itread01 2020-11-06 01:17:55
python appium automatic operation wechat


## 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 : ![](https://img2020.cnblogs.com/blog/1497257/202011/1497257-20201104202048842-190799949.png) * 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 : ![](https://img2020.cnblogs.com/blog/1497257/202011/1497257-20201104202103416-304964754.png) ### 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 : ![](https://img2020.cnblogs.com/blog/1497257/202011/1497257-20201104202116413-1758267038.png) After startup, it is shown in the figure : ![](https://img2020.cnblogs.com/blog/1497257/202011/1497257-20201104202128074-1894379158.png) 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 : ![](https://img2020.cnblogs.com/blog/1497257/202011/1497257-20201104202143232-1684553235.png) Click to enter the following interface : ![](https://img2020.cnblogs.com/blog/1497257/202011/1497257-20201104202154273-1578964666.png) 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 : ![](https://img2020.cnblogs.com/blog/1497257/202011/1497257-20201104202205334-119776773.png) 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 : ![](https://img2020.cnblogs.com/blog/1497257/202011/1497257-20201104202215682-1653597752.png) 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 : ![](https://img2020.cnblogs.com/blog/1497257/202011/1497257-20201104202226884-1118317961.png) Click and then go to Appium Click the refresh button , As shown in the figure below : ![](https://img2020.cnblogs.com/blog/1497257/202011/1497257-20201104202236873-1484534303.png) 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]所创,转载请带上原文链接,感谢

  1. 利用Python爬虫获取招聘网站职位信息
  2. Using Python crawler to obtain job information of recruitment website
  3. Several highly rated Python libraries arrow, jsonpath, psutil and tenacity are recommended
  4. Python装饰器
  5. Python实现LDAP认证
  6. Python decorator
  7. Implementing LDAP authentication with Python
  8. Vscode configures Python development environment!
  9. In Python, how dare you say you can't log module? ️
  10. 我收藏的有关Python的电子书和资料
  11. python 中 lambda的一些tips
  12. python中字典的一些tips
  13. python 用生成器生成斐波那契数列
  14. python脚本转pyc踩了个坑。。。
  15. My collection of e-books and materials about Python
  16. Some tips of lambda in Python
  17. Some tips of dictionary in Python
  18. Using Python generator to generate Fibonacci sequence
  19. The conversion of Python script to PyC stepped on a pit...
  20. Python游戏开发,pygame模块,Python实现扫雷小游戏
  21. Python game development, pyGame module, python implementation of minesweeping games
  22. Python实用工具,email模块,Python实现邮件远程控制自己电脑
  23. Python utility, email module, python realizes mail remote control of its own computer
  24. 毫无头绪的自学Python,你可能连门槛都摸不到!【最佳学习路线】
  25. Python读取二进制文件代码方法解析
  26. Python字典的实现原理
  27. Without a clue, you may not even touch the threshold【 Best learning route]
  28. Parsing method of Python reading binary file code
  29. Implementation principle of Python dictionary
  30. You must know the function of pandas to parse JSON data - JSON_ normalize()
  31. Python实用案例,私人定制,Python自动化生成爱豆专属2021日历
  32. Python practical case, private customization, python automatic generation of Adu exclusive 2021 calendar
  33. 《Python实例》震惊了,用Python这么简单实现了聊天系统的脏话,广告检测
  34. "Python instance" was shocked and realized the dirty words and advertisement detection of the chat system in Python
  35. Convolutional neural network processing sequence for Python deep learning
  36. Python data structure and algorithm (1) -- enum type enum
  37. 超全大厂算法岗百问百答(推荐系统/机器学习/深度学习/C++/Spark/python)
  38. 【Python进阶】你真的明白NumPy中的ndarray吗?
  39. All questions and answers for algorithm posts of super large factories (recommended system / machine learning / deep learning / C + + / spark / Python)
  40. [advanced Python] do you really understand ndarray in numpy?
  41. 【Python进阶】Python进阶专栏栏主自述:不忘初心,砥砺前行
  42. [advanced Python] Python advanced column main readme: never forget the original intention and forge ahead
  43. python垃圾回收和缓存管理
  44. java调用Python程序
  45. java调用Python程序
  46. Python常用函数有哪些?Python基础入门课程
  47. Python garbage collection and cache management
  48. Java calling Python program
  49. Java calling Python program
  50. What functions are commonly used in Python? Introduction to Python Basics
  51. Python basic knowledge
  52. Anaconda5.2 安装 Python 库(MySQLdb)的方法
  53. Python实现对脑电数据情绪分析
  54. Anaconda 5.2 method of installing Python Library (mysqldb)
  55. Python implements emotion analysis of EEG data
  56. Master some advanced usage of Python in 30 seconds, which makes others envy it
  57. python爬取百度图片并对图片做一系列处理
  58. Python crawls Baidu pictures and does a series of processing on them
  59. python链接mysql数据库
  60. Python link MySQL database