zoukankan      html  css  js  c++  java
  • 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()

    转载请附上原文链接。
  • 相关阅读:
    记录log中的16进制和ASCII码字符输出
    有效的沟通技巧
    时间的真谛
    目标设定与时间管理
    第四代时间管理
    什么是高效沟通
    error LNK1104: cannot open file 错误解决方案
    js压缩工具1.0界面绘制
    时间管理的定义与目的
    JArgs命令行选项解析>Java套件
  • 原文地址:https://www.cnblogs.com/bugbreak/p/12028075.html
Copyright © 2011-2022 走看看