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()

猜你喜欢

转载自www.cnblogs.com/bugbreak/p/12028075.html