1.List定位
from appium import webdriver
from time import sleep
caps = {}
caps["platformName"] = "Android"
caps["platformVersion"] = "5.1.1"
caps["deviceName"] = "127.0.0.1:62001"
caps["appPackage"] = "com.tal.kaoyan"
caps["appActivity"] = ".ui.activity.ucenter.LoginActivity"
caps["notReset"]=True
driver=webdriver.Remote('http://127.0.0.1:4723/wd/hub',caps)
driver.implicitly_wait(5)
# 点击注册
driver.find_element_by_id('com.tal.kaoyan:id/login_register_text').click()
# 获取整个页面的父级
eles=driver.find_element_by_id('com.tal.kaoyan:id/activity_register_parentlayout')
# 点击添加头像
eles.find_element_by_id('com.tal.kaoyan:id/activity_register_userheader').click()
images=driver.find_elements_by_id('com.tal.kaoyan:id/item_image')
images[1].click
2.Xpath定位
xpath定位是一种路径定位方式,主要是依赖于元素绝对路径或者相关属性来定位,但是绝对路径xpath执行效率比较低(特别是元素路径比较深的时候),一般使用比较少。通常使用xpath相对路径和属性定位。
from appium import webdriver
from time import sleep
caps = {}
caps["platformName"] = "Android"
caps["platformVersion"] = "5.1.1"
caps["deviceName"] = "127.0.0.1:62001"
caps["appPackage"] = "com.tal.kaoyan"
caps["appActivity"] = ".ui.activity.ucenter.LoginActivity"
caps["notReset"]=True
driver=webdriver.Remote('http://127.0.0.1:4723/wd/hub',caps)
driver.implicitly_wait(5)
# driver.find_element_by_xpath('//android.widget.EditText[@resource-id=\"com.tal.kaoyan:id/login_email_edittext\"]').send_keys('lebo')
driver.find_element_by_xpath('//android.widget.EditText[@text="请输入用户名"]').send_keys('test')
driver.find_element_by_xpath('//android.widget.EditText[@index="3" and @resource-id="com.tal.kaoyan:id/login_password_edittext"]').send_keys(111111)
driver.find_element_by_xpath('//android.widget.EditText').send_keys(111111)
driver.find_element_by_xpath('//*[@text="登录"]').click()
# 模糊匹配
driver.find_element_by_xpath("//*[contains(@text,'登')]").click()
3.Uiautomator定位
UIAutomator元素定位是 Android 系统原生支持的定位方式,虽然与 xpath 类似,但比它更加好用,且支持元素全部属性定位.定位原理是通过android 自带的android uiautomator的类库去查找元素。 Appium元素定位方法其实也是基于Uiautomator来进行封装的。 使用方法 find_element_by_android_uiautomator() 可以运用UiAutomator元素定位。
from appium import webdriver
from time import sleep
caps = {}
caps["platformName"] = "Android"
caps["platformVersion"] = "5.1.1"
caps["deviceName"] = "127.0.0.1:62001"
caps["appPackage"] = "com.tal.kaoyan"
caps["appActivity"] = ".ui.activity.ucenter.LoginActivity"
caps["notReset"]=True
driver=webdriver.Remote('http://127.0.0.1:4723/wd/hub',caps)
driver.implicitly_wait(5)
driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.tal.kaoyan:id/login_password_edittext")').send_keys('leboxiaomei')
driver.find_element_by_android_uiautomator('new UiSelector().text("请输入用户名")').send_keys('leboxiaomei')
driver.find_element_by_android_uiautomator('new UiSelector().className("android.widget.Button")').click()
连接夜神新版本的模拟器
1.查看端口:进入夜神安装目录执行 nox_adb devices
2.直接拖动图片到模拟器,弹出窗口关闭即可
QQ应用
from appium import webdriver
from time import sleep
from appium.webdriver.common.touch_action import TouchAction
caps = {}
caps["platformName"] = "Android"
caps["platformVersion"] = "5.1.1"
caps["deviceName"] = "127.0.0.1:62001"
caps["appPackage"] = "com.tencent.mobileqq"
caps["appActivity"] = '.activity.LoginActivity'
caps["notReset"]=True
driver=webdriver.Remote('http://127.0.0.1:4723/wd/hub',caps)
driver.implicitly_wait(30)
# 提取contenet-desc 字段进行定位
driver.find_element_by_accessibility_id('请输入QQ号码或手机或邮箱').send_keys(12345)
微信 发送语音
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
caps = {}
caps["platformName"] = "Android"
caps["platformVersion"] = "5.1.1"
caps["deviceName"] = "127.0.0.1:62001"
caps["appPackage"] = "com.tencent.mm"
caps["appActivity"] = ".ui.LauncherUI"
caps["noReset"]=True
driver=webdriver.Remote('http://127.0.0.1:4723/wd/hub',caps)
driver.implicitly_wait(20)
# 聊天列表选择第一个联系人
driver.find_element_by_id('com.tencent.mm:id/azl').click()
try:
# 定位语音按钮
driver.find_element_by_id('com.tencent.mm:id/aif')
except:
# 点击切换到语音模式
driver.find_element_by_id('com.tencent.mm:id/aic').click()
speak = driver.find_element_by_id('com.tencent.mm:id/aif')
# 发送5s语音
TouchAction(driver).long_press(el=speak, duration=5000).release().perform()
# 点击切换到文本模式
driver.find_element_by_id('com.tencent.mm:id/aic').click()
# 输入框输入内容
driver.find_element_by_id('com.tencent.mm:id/aie').send_keys('test')
# 点击发送按钮
driver.find_element_by_id('com.tencent.mm:id/aik').click()
|