Appium基础篇9-元素定位之by_xpath

        我们继续来介绍Appium元素定位方法,有些人做过selenium自动化测试,对xpath元素定位很熟悉。他突然想用xpath来定义手机app页面元素,那么这个是否可以呢?我们在IDE环境输入driver.,后面就自动弹出各种方法,也看到了确实有xpath元素定位。本篇就来介绍xpath元素定位,一般来说,元素定位选择第一是ID,第二是class,如果class没有重复,第三是by uiautomator,第四,可能就是xpath。前面三种方法都无法定位元素的时候,xpath就闪亮登场。我们通过xpath定位手机百度底部菜单栏中的我的模块来演示xpath的写法。

1.打开UI Automator Viewer,抓取我的 元素信息。

       通过这个软件,我们发现底部这一排菜单都是同一个class,唯一不同就是界面上图标不同,和index不同,这里index是索引的意思。但是这个主意哈,当前我的索引是4,底部一共有5个按钮,说明索引是从0开始算。本篇我们要学习xpath定位,但是xpath索引是从1开始计算的。利用xpath定位的思路是:先根据class定位到五个元素,然后根据索引5来精确定位到“我的”模块。

2. Python+Appium脚本实现过程。

import os

import time
from appium import webdriver

apk_path = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))  # 获取当前项目的根路径

desired_caps ={}
desired_caps['platformName'] = 'Android' #设备系统
desired_caps['platformVersion'] = '6.0.1' #设备系统版本
desired_caps['deviceName'] = 'KIW-AL10' #设备名称

# 测试apk包的路径
desired_caps['app'] = apk_path + '\\app\\shoujibaidu.apk'
# 不需要每次都安装apk
desired_caps['noReset'] = True
# 应用程序的包名
desired_caps['appPackage'] = 'com.baidu.searchbox'
desired_caps['appActivity'] = 'com.baidu.searchbox.SplashActivity'

driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)#启动app
time.sleep(5) #app启动后等待5秒,方便元素加载完成
# 根据元素xpath来定位
# 点击“我的”
my_home = driver.find_element_by_xpath("//*[@class='android.widget.FrameLayout'][5]").click()
time.sleep(2)


       通过运行发现,确定定位到了我的模块,看手机效果可以证明XPATH表达式定位是成功的。为了练习xpath,我们再次用xpath来定位搜索输入框元素。你可以修改下索引数字,来点击底部其余4个主菜单。

我们来试试,利用ui automator viewer给出的index的值来定位,发现有些定位不准,定位到我的模块是没问题的。

import os

import time
from appium import webdriver

apk_path = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))  # 获取当前项目的根路径

desired_caps ={}
desired_caps['platformName'] = 'Android' #设备系统
desired_caps['platformVersion'] = '6.0.1' #设备系统版本
desired_caps['deviceName'] = 'KIW-AL10' #设备名称

# 测试apk包的路径
desired_caps['app'] = apk_path + '\\app\\shoujibaidu.apk'
# 不需要每次都安装apk
desired_caps['noReset'] = True
# 应用程序的包名
desired_caps['appPackage'] = 'com.baidu.searchbox'
desired_caps['appActivity'] = 'com.baidu.searchbox.SplashActivity'

driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)#启动app
time.sleep(3) #app启动后等待3秒,方便元素加载完成
# 根据元素xpath来定位
# 点击“我的”
driver.find_element_by_xpath("//*[@class='android.widget.FrameLayout' and @index='4']").click()
time.sleep(2)
# 点击“我的关注“
driver.find_element_by_xpath("//*[@class='android.widget.FrameLayout' and @index='3']").click()
time.sleep(2)
# 点击“麦克风“
driver.find_element_by_xpath("//*[@class='android.widget.FrameLayout' and @index='2']").click()
time.sleep(2)
# 点击““视频
driver.find_element_by_xpath("//*[@class='android.widget.FrameLayout' and @index='1']").click()
time.sleep(2)
# 点击“默认主页“
driver.find_element_by_xpath("//*[@class='android.widget.FrameLayout' and @index='0']").click()
time.sleep(2)
       这样来回切换点击是运行不成功,如果每次只点击一个,发现点击麦克风的index是1,但是ui automator viewer给出的是index是2,这个地方是有问题的。

总结:

      很多人都说xpath运行很慢,不建议使用,这个观点我基本同意,因为如果id class都定位不了,我们可以考虑用xpath来解决。




  • 3
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值