appium-app页面元素定位
·
一、appium的元素定位方式有
1、通过id定位:
resource-id属性是id:

driver.find_element_by_id("com.sina.weibo:id/et_pws_username")
2、通过ClassName定位:
class

driver.find_element_by_class_name("android.widget.EditText");
3、通过Accessibilityid定位:content-desc

driver.find_element_by_accessibility_id("com.sina.weibo:id/et_pws_username")
4、通过AndroidUiAutomator定位
AndroidUIAutomator是一个强有力的元素定位方式,它是通过Android UIAutomator类库去找元素,可以选择id、name、className作为传入的字符串,该方法的参数为UiSelector类定位元素表达式:
new UiSelector().函数名称(“定位表达式”)
示例:
driver.find_element_by_android_uiautomator('new UiSelector().resourceld(\"com.sina.weibo:id/et_pws_username")')
5、通过xpath定位:xpath

WebElement element = driver.findElement(By.xpath("//android.widget.EditText[@resource-id='com.sina.weibo:id/et_pws_username']"));
初级实例
from appium import webdriver
import pytest
import time
desired_caps = {
'platformName': 'Android',
'deviceName': '127.0.0.1:62001',
'platformVersion': '5.1.1',
# apk包名
'appPackage': 'com.sina.weibo',
# apk的launcherActivity
'appActivity': 'com.sina.weibo.VisitorMainTabActivity',
'autoGrantPermissions':True, # 设置自动授权权限
'unicodeKeyboard' : True, # 输入中文时要加,要不然输入不了中文
'resetKeyboard' : True # 输入中文时要加,要不然输入不了中文
}
#连接appium server,appium desktop要启动,有监听端口
#要将服务器参数发送过去
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
driver.implicitly_wait(20)
#运行代码之前:appium server启动成功,处于监听状态,
# 2.模拟器/真机必须被电脑识别,即adb devices能识别要操作的设备
#点击“用账号登录”
driver.find_element_by_id("com.sina.weibo:id/tv_for_psw_login").click()
#输入手机号
driver.find_element_by_id("com.sina.weibo:id/et_pws_username").send_keys("1884645xxxx")
#输入密码
driver.find_element_by_id("com.sina.weibo:id/et_pwd").send_keys("xxxxxxxxxx")
#点击登录
driver.find_element_by_id("com.sina.weibo:id/bn_pws_Login").click()
添加判断元素是否存在的代码
from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import pytest
import time
desired_caps = {
'platformName': 'Android',
'deviceName': '127.0.0.1:62001',
'platformVersion': '5.1.1',
# apk包名
'appPackage': 'com.sina.weibo',
# apk的launcherActivity
'appActivity': 'com.sina.weibo.VisitorMainTabActivity',
'autoGrantPermissions':True, # 设置自动授权权限
'unicodeKeyboard' : True, # 输入中文时要加,要不然输入不了中文
'resetKeyboard' : True # 输入中文时要加,要不然输入不了中文
}
#连接appium server,appium desktop要启动,有监听端口
#要将服务器参数发送过去
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
#运行代码之前:appium server启动成功,处于监听状态,
# 2.模拟器/真机必须被电脑识别,即adb devices能识别要操作的设备
#判断“用账号登录”按钮是否存在
WebDriverWait(driver,20).until(EC.visibility_of_element_located((MobileBy.ID,'com.sina.weibo:id/tv_for_psw_login')))
#点击“用账号登录”
driver.find_element_by_id("com.sina.weibo:id/tv_for_psw_login").click()
#判断输入手机号框是否存在
WebDriverWait(driver,20).until(EC.visibility_of_element_located((MobileBy.ID,'com.sina.weibo:id/et_pws_username')))
#输入手机号
driver.find_element_by_id("com.sina.weibo:id/et_pws_username").send_keys("18846450275")
#输入密码
driver.find_element_by_id("com.sina.weibo:id/et_pwd").send_keys("0322liudany")
#点击登录
driver.find_element_by_id("com.sina.weibo:id/bn_pws_Login").click()
更多推荐
所有评论(0)