Appium基础篇10-元素定位之by_accessibility_id

  本篇来介绍另外一种元素定位方式,叫by_accessibility_id,它是一个什么鬼呢?因为我们在UI Automator Viewer界面上并没有找到这个字段,我可以告诉你,这个对应的字段是content-desc,结果找了很久,没有在手机百度找到这个类型的来举例。最后,在天猫的app找到了,而且天猫app很多元素可以用这种方式来定位。

1.手机安装天猫app,利用前面文章知识获取包名和 Activity

[python]  view plain  copy
  1. desired_caps['appActivity'] = 'com.tmall.wireless.splash.TMSplashActivity'  

2.UI automator viewer抓元素
 
3.Python脚本实现元素定位
[python]  view plain  copy
  1. <span style="font-size:14px;color:#000000;">import os  
  2. import time  
  3. from appium import webdriver  
  4.   
  5. apk_path = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))  # 获取当前项目的根路径  
  6.   
  7. desired_caps ={}  
  8. desired_caps['platformName'] = 'Android' #设备系统  
  9. desired_caps['platformVersion'] = '6.0.1' #设备系统版本  
  10. desired_caps['deviceName'] = 'KIW-AL10' #设备名称  
  11.   
  12. # 测试apk包的路径  
  13. desired_caps['app'] = apk_path + '\\app\\shoujibaidu.apk'  
  14. # 不需要每次都安装apk  
  15. desired_caps['noReset'] = True  
  16. # 应用程序的包名  
  17. desired_caps['appPackage'] = 'com.tmall.wireless'  
  18. desired_caps['appActivity'] = 'com.tmall.wireless.splash.TMSplashActivity'  
  19.   
  20. driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)#启动app  
  21. time.sleep(3#app启动后等待3秒,方便元素加载完成  
  22. # 根据元素 content-desc来定位  
  23. # 点击“天猫超市”  
  24. driver.find_element_by_accessibility_id("天猫超市").click()</span>  
运行,测试成功。

猜你喜欢

转载自blog.csdn.net/qq_24857309/article/details/79664386