1、name定位
driver.find_element_by_id('com.shanjian.originaldesign:id/edit_Tel').send_keys('')
driver.find_element_by_id('com.shanjian.originaldesign:id/edit_Pwd').send_keys('')
driver.find_element_by_name('登录').click()
所以,我们暂时可以把元素的text 属性看作name 来使用。如果你的app不能正常的定位元素,那就换其它定位方式吧。
2).id定位
driver.find_element_by_id('com.shanjian.originaldesign:id/edit_Tel').send_keys('15817252876') driver.find_element_by_id('com.shanjian.originaldesign:id/edit_Pwd').send_keys('123456') driver.find_element_by_name('登录').click()
通过UI Automator定位到的resource-id就是元素的ID、
3).classname定位
desired_caps = {
'platformName': 'Android',
'platformVersion': '5.0.0.0',
'deviceName': '127.0.0.1:62001',
'appPackage': 'com.shanjian.originaldesign',
'appActivity':'.activity.other.Activity_In'}
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
sleep(10)
button = driver.find_elements_by_class_name("android.widget.EditText")
print len(button)
输出结果:
(注:一般一个页面上的class属性不唯一,元素不唯一的话定位会报错了)
4).uiautomator定位
desired_caps = {
'platformName': 'Android',
'platformVersion': '5.0.0.0',
'deviceName': '127.0.0.1:62001',
'appPackage': 'com.shanjian.originaldesign',
'appActivity':'.activity.other.Activity_In',
}
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
sleep(10)
#用于清除历史记录
driver.find_element_by_id("com.shanjian.originaldesign:id/edit_Tel").clear()
driver.find_element_by_android_uiautomator('new UiSelector().text("输入手机号码")').send_keys("15817252876")
driver.find_element_by_id("com.shanjian.originaldesign:id/edit_Pwd").send_keys("123456")
driver.find_element_by_android_uiautomator('new UiSelector().text("登录")').click()
注意:使用uiautomator定位时new UiSelect()中间有空格,并且最外层使用单引号,里面使用双引号,否则会报如下错误:
selenium.common.exceptions.WebDriverException: Message: The requested resource could not be found, or a request was received using an HTTP method that is not supported by the mapped resource.
driver.find_element_by_android_uiautomator("new UiSelector().text('登录')").click()
5).accessibility_id定位
1.accessibility_id定位就是通过UI Automator工具查看的content-desc属性
(当然下图这个元素的content-desc属性为空,就不能通过这个属性定位了,这里只是举个例子)
2.如上图的定位,看到content-desc属性有值,就可以通过accessibility_id来定位了
格式:driver.find_element_by_accessibility_id(“content-desc的值”)
driver.find_element_by_accessibility_id(“xxx”)
Tip:content-desc值对应在web页面中就是text值,appium使用accessibility_id来定位
6).Xpath定位
需要注意的是:在appium中使用Xpath定位,标签名就是class的值。
sleep(10)
driver.find_element_by_id("com.shanjian.originaldesign:id/edit_Tel").clear()
driver.find_element_by_xpath("//android.widget.EditText[@resource-id='com.shanjian.originaldesign:id/edit_Tel']").send_keys('15817252876')
sleep(10)
driver.find_element_by_xpath("//android.widget.EditText[@resource-id='com.shanjian.originaldesign:id/edit_Pwd']").send_keys('123456')
sleep(10)
driver.find_element_by_xpath("//android.widget.TextView[@text='登录']").click()
from selenium.webdriver.common.by import By
driver.find_element_by_xpath("//android.widget.EditText[contains(@resource-id,'originaldesign')]").send_keys('15817252876')
driver.find_element(By.XPATH,("//android.widget.EditText[contains(@resource-id,'originaldesign')]")).send_keys('15817252876')
7).其他定位方法
find_element_by_tag_name()
find_element_by_link_text()
find_element_by_partial_link_text()
find_element_by_css_selector()