自动化微博

前提条件

先自己手动登录微博账号

定位app的Package Name 和 Activity Name

  1. adb shell # 进入shell命令中
  2. logcat | grep START # 过滤启动的activity

image.png

  • package name : com.sina.weibo
  • activename : .SplashActivity

启动配置

  1. 启动配置

元素定位

使用appium 进行元素定位

  1. from appium import webdriver
  2. from appium.webdriver.common.touch_action import TouchAction
  3. desired_caps = {
  4. 'platformName': 'Android', # 测试Android系统
  5. 'platformVersion': '7.1.2', # Android版本 可以在手机的设置中关于手机查看
  6. 'deviceName': '127.0.0.1:62001', # adb devices 命令查看 设置为自己的设备
  7. 'automationName': 'UiAutomator2', # 自动化引擎
  8. 'noReset': True, # 不要重置app的状态
  9. 'fullReset': False, # 不要清理app的缓存数据
  10. 'appPackage':"com.sina.weibo", # 应用的包名
  11. 'appActivity': ".SplashActivity" # 应用的活动页名称
  12. }
  13. driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_capabilities=desired_caps)
  14. import time
  15. # 等待页面加载成功
  16. time.sleep(10)
  17. # This sample code uses the Appium python client
  18. # pip install Appium-Python-Client
  19. # Then you can paste this into a file and simply run with Python
  20. el1 = driver.find_element_by_id("com.sina.weibo:id/rightBtn_wrapper")
  21. el1.click()
  22. el2 = driver.find_element_by_accessibility_id("写微博")
  23. el2.click()
  24. # 等待发微博页面加载成功
  25. time.sleep(1)
  26. el3 = driver.find_element_by_id("com.sina.weibo:id/edit_view")
  27. el3.send_keys("Hi 我在使用自动化方式发布微博")
  28. el4 = driver.find_element_by_accessibility_id("发送")
  29. el4.click()
  30. TouchAction(driver).press(x=390, y=200).move_to(x=390, y=1323).release().perform()
  31. # driver.quit()

添加图片

  1. from appium import webdriver
  2. from appium.webdriver.common.touch_action import TouchAction
  3. desired_caps = {
  4. 'platformName': 'Android', # 测试Android系统
  5. 'platformVersion': '7.1.2', # Android版本 可以在手机的设置中关于手机查看
  6. 'deviceName': '127.0.0.1:62001', # adb devices 命令查看 设置为自己的设备
  7. 'automationName': 'UiAutomator2', # 自动化引擎
  8. 'noReset': True, # 不要重置app的状态
  9. 'fullReset': False, # 不要清理app的缓存数据
  10. 'appPackage':"com.sina.weibo", # 应用的包名
  11. 'appActivity': ".SplashActivity" # 应用的活动页名称
  12. }
  13. driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_capabilities=desired_caps)
  14. import time
  15. # 等待页面加载成功
  16. time.sleep(10)
  17. # This sample code uses the Appium python client
  18. # pip install Appium-Python-Client
  19. # Then you can paste this into a file and simply run with Python
  20. el1 = driver.find_element_by_id("com.sina.weibo:id/rightBtn_wrapper")
  21. el1.click()
  22. time.sleep(1)
  23. el2 = driver.find_element_by_accessibility_id("写微博")
  24. el2.click()
  25. # 等待发微博页面加载成功
  26. time.sleep(1)
  27. el3 = driver.find_element_by_id("com.sina.weibo:id/edit_view")
  28. el3.send_keys("Hi 我在使用自动化方式发布微博")
  29. driver.find_element_by_accessibility_id("插入图片").click()
  30. time.sleep(3)
  31. # 选择图片
  32. xpath_img1 = '//*[@resource-id="com.sina.weibo:id/photo_album_gridview"]/android.widget.RelativeLayout[2]'
  33. xpath_img2 = '//*[@resource-id="com.sina.weibo:id/photo_album_gridview"]/android.widget.RelativeLayout[3]'
  34. driver.find_element_by_xpath(xpath_img1).click()
  35. time.sleep(2)
  36. # 选中
  37. driver.find_element_by_id('com.sina.weibo:id/btn_num_check').click()
  38. # 返回上一层
  39. driver.back()
  40. time.sleep(2)
  41. driver.find_element_by_xpath(xpath_img2).click()
  42. time.sleep(1)
  43. driver.find_element_by_id('com.sina.weibo:id/btn_num_check').click()
  44. # 点击下一步
  45. driver.find_element_by_id('com.sina.weibo:id/btn_confirm_edit').click()
  46. time.sleep(1)
  47. driver.find_element_by_id('com.sina.weibo:id/btn_confirm_edit').click()
  48. time.sleep(1)
  49. el4 = driver.find_element_by_accessibility_id("发送")
  50. el4.click()
  51. TouchAction(driver).press(x=390, y=200).move_to(x=390, y=1323).release().perform()
  52. # driver.quit()

image.png