• python+Appium自动化:各种元素定位方法


    name定位

     driver.find_element_by_name('飞利浦净水').click()

    测试结果报错:selenium.common.exceptions.InvalidSelectorException: Message: Locator Strategy 'name' is not supported for this session

    一开始以为写错了,后面通过搜索资料才知道,name这个定位方法,appium从1.5版本后就已经抛弃了。。。

    所以可以通过其他定位方式来完成。

    classname定位

    driver.find_element_by_class_name('android.widget.TextView').click()

    这种定位方式也是有弊端的,如果当界面class元素是唯一的时候是可以用的,但是如果页面有多个class是一样的,这时候就可能只操作页面元素的第一个。

    相对定位

    先找到元素的父元素节点,再通过父元素进行元素定位

    root_element=driver.find_element_by_id('id')
    root_element.find_element_by_class_name('android.widget.TextView').click()

    Xpath定位

    通过id

    driver.find_element_by_xpath('//*[@resource-id="com.taobao.taobao:id/home_searchedit"]').click()

    通过text

    driver.find_element_by_xpath('//*[@text="XXXX"]').click()

    通过class,两种写法

    driver.find_element_by_xpath('//android.widget.TextView’).click()

    driver.find_element_by_xpath('//[class="android.widget.TextView"]').click()

    通过组合

    driver.find_element_by_xpath('//android.widget.TextView[@text="XXXX"]’).click()

    driver.find_element_by_xpath('//*[@text="XXXX" and @index="num"]').click()

    List定位

    主要使用find_elements_by_XXX来获取一组相同class属性的元素,然后通过数组下标来控制的定位。

     比如如上截图,定位到飞利浦标签的时候,有很多个相同的TextView,这时候可以这样来定位,先用xpath定位到此元素,再用数据

    d=driver.find_elements_by_xpath('//android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.View/android.support.v7.widget.RecyclerView/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.TextView')
    d[0].click() #定位到飞利浦净水

    d[1].click() #定位到行李箱

    UIautomator定位

    是Android系统原生支持的定位方式,和xpath类似,且支持元素的所有属性定位,Appium元素定位也是基于Uiautomator进行封装的,so定位也是很强大。

    使用到方法是find_element_by_android_uiautomator(),还有UiSelector()

    定位搜索框为例子:

    #id定位

    driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.taobao.taobao:id/home_searchedit")').click()

    #text定位

    driver.find_element_by_android_uiautomator('new UiSelector().text("超滤净水器 家用")').click()

    #classname定位(如果有多个class元素则定位第一个)

    driver.find_element_by_android_uiautomator('new UiSelector().className("android.widget.TextView")').click()

    转载请附上原文链接。
  • 相关阅读:
    linq to access 简单实现 实例demo
    FCKEDITOR中文使用说明 js调用
    asp.net mvc 随想
    fccms 小型简单个人blog源码
    PHP文件上传路径
    前端优化技巧(一)
    会话框拖拽效果实现
    phpmailer配置
    上传图片动态预览(兼容主流浏览器)
    Java将多个list对象根据属性分组后合并成一个新的集合
  • 原文地址:https://www.cnblogs.com/bugbreak/p/12028075.html
Copyright © 2020-2023  润新知