如何定位一个元素
通过id去找到元素
driver.findElementById(" ").click();
通过name属性找到元素
driver.findElementByName("").click();
通过XPath属性找到元素
driver.findElementByXPath("//android.widget.TestView[@text='name']").click();
如何定位一组元素
driver.findElementsById(" ").click();
driver.findElementsByName("").click();
driver.findElementsByXPath("//android.widget.TestView[@text='name']").click();
返回按钮
driver.sendKeyEvent(AndroidKeyCode.BACK);
元素等待
隐式等待
driver.implicitly_wait(时间)
显式等待
wait = WebDriverWait(Drive, 5)
back_button = wait.until(lambda x: x.find....表达式)
back_button.click()
区别:隐式等待为全局元素,显式等待为单个元素有效。
元素操作API(element是获取到的元素)
点击元素
element.click()
输入和清空输入框内容
element.sendkeys(value) 输入
element.clear() 清空
注意:需要设置支持输入中文和特殊字符
DesiredCapabilities capabilities = new DesiredCapabilities();
设置使用unicode键盘,支持输入中文和特殊字符
capabilities.setCapability("unicodeKeyboard","true");
设置用例执行完成后重置键盘
capabilities.setCapability("resetKeyboard","true");
获取元素的文本内容
element.text
根据属性名获取属性值
else = driver.findElementsById("com.android.settings:id/title");
for i in else:
print(i.getattribute("enabled"))
滑动和拖拽事件
1、使用swipe滑动屏幕:从一个位置到另一个位置(有惯性)
driver.swipe(start_x, start_y, end_x, end_y, duration=时间(毫秒))
//从(100,2000)位置滑到(100, 1000)的位置
driver.swipe(100, 2000, 100, 1000)
2、scrpll滑动事件:从一个元素到另一个元素(惯性大)
driver.scroll(开始元素,结束元素)
3、drag_and_drop拖拽事件:从一个元素到另一个元素(没有惯性)
driver.drag_and_drop(开始元素,结束元素)
网友评论