Xpath定位进阶
层级定位
- 父结点定位子节点
- 子节点定位父节点
- 子节点定位兄弟节点
- 爷爷节点定位孙子节点
例子
import time
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
class TestElementOperator:
def setup(self):
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['deviceName'] = 'true'
desired_caps['appPackage'] = 'com.xueqiu.android'
desired_caps['appActivity'] = '.view.WelcomeActivityAlias'
desired_caps['noReset'] = "true"
self.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)
self.driver.implicitly_wait(10)
def teardown(self):
self.driver.quit()
def test_stock_price(self):
# 点击输入框
self.driver.find_element_by_id('com.xueqiu.android:id/tv_search').click()
# 向输入框输入 阿里巴巴
self.driver.find_element_by_id('com.xueqiu.android:id/search_input_text').send_keys('阿里巴巴')
# 点击第一个搜索结果
self.driver.find_element_by_xpath(
"//*[@resource-id='com.xueqiu.android:id/name' and @text='阿里巴巴']").click()
# 获取股票价格
stock_price = self.driver.find_element_by_xpath(
'//*[@text="09988"]/../../..//*[@resource-id="com.xueqiu.android:id/current_price"]').text
print(stock_price)
time.sleep(10)
uiautomator定位
优点:
- xpath定位速度慢
- uiautomator是Android的工作引擎,速度快
缺点:表达式书写复杂,容易写错IDE没有提示
参考链接:
- https://developer.android.com/reference/android/support/test/uiautomator/UiSelector.html
例子
import time
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
class TestElementOperator:
def setup(self):
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['deviceName'] = 'true'
desired_caps['appPackage'] = 'com.xueqiu.android'
desired_caps['appActivity'] = '.view.WelcomeActivityAlias'
desired_caps['noReset'] = "true"
self.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)
self.driver.implicitly_wait(10)
def teardown(self):
self.driver.quit()
def test_login(self):
# 点击我的,进入到个人信息页面
# 点击登录,进入到登录页面
# 输入用户名,输入密码
self.driver.find_element_by_android_uiautomator(
'new UiSelector().text("我的").resourceId("com.xueqiu.android:id/tab_name")').click()
self.driver.find_element_by_android_uiautomator('new UiSelector().text("登录雪球")').click()
self.driver.find_element_by_android_uiautomator(
'new UiSelector().resourceId("com.xueqiu.android:id/login_account")').send_keys('username')
self.driver.find_element_by_android_uiautomator(
'new UiSelector().resourceId("com.xueqiu.android:id/login_password")').send_keys('password')
time.sleep(5)
写法
- driver.find_element_by_android_uiautomator(‘表达式’)
表达式写法:
-
通过resourceid定位
– new UiSelector().resourceId(“id”) -
通过classname定位
– new UiSelector().className(‘className’) -
通过content-desc定位
– new UiSelector().description(“content-desc属性”) -
通过text文本定位
– new UiSelector().text(“text文本”) -
文本比较长,可以用textContains模糊匹配
– new UiSelector().textContains(“包含text文本”) -
同样可以用textStartsWith是以某个文本开头来匹配
– new UiSelector().textStartsWith(“以text文本开头”) -
用正则表达式textMatches匹配
– new UiSelector().textMatches(“正则表达式”) -
父子关系定位:先定位到父元素,通过父元素找儿子
– son=‘new UiSelector().resourceId(“com.baidu.yuedu:id/rl_tabs”).childSelector(text(“股票”))’ -
兄弟定位
– brother=‘new UiSelector().resourceId(“com.baidu.yuedu:id/lefttitle”).fromParent(text(“股票”))’ -
组合定位
– id和text属性组合
— id_text=‘new UiSelector().resourceId(“表达式”)’.text(“表达式”)
— driver.find_element_by_android_uiautomator(id_text).click()
– class与text属性组合
— class_text=‘new UiSelector().className(“表达式”).text(“表达式”)’
— driver.find_element_by_android_uiautomator(class_text).click()
实现滚动查找元素
‘new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text(“查找的文本”).instance(0));’
例子
import time
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
class TestElementOperator:
def setup(self):
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['deviceName'] = 'true'
desired_caps['appPackage'] = 'com.xueqiu.android'
desired_caps['appActivity'] = '.view.WelcomeActivityAlias'
desired_caps['noReset'] = "true"
self.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)
self.driver.implicitly_wait(10)
def teardown(self):
self.driver.quit()
def test_scroll_find_element(self):
self.driver.find_element_by_android_uiautomator(
'new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text("安娜2012").instance(0));'
).click()