Toast 判断-----基本操作问题
首先基本操作,进入安卓市场的账号密码页面---
from appium importwebdriverfrom selenium.webdriver.support.ui importWebDriverWaitfrom selenium.webdriver.support importexpected_conditions as ECfrom appium.webdriver.common.touch_action import TouchAction #导入Touch Action类
importtime,re,os
Start={}
Start['platformName']='android' #设备型号 android或者ios
Start['platformVersion']='5.1' #安卓设备版本号
Start['deviceName']='192.168.176.101:5555' #安卓设备名称
Start['app']=r'C:\Users\Administrator\Desktop\xuexi\apk\anzhuoshichang_16793302.apk' #设备路径 pc电脑存放apk包的路径#上面的路径不推荐这种写法,但是目前先这么写!后续告诉大家正确的写法
Start['appPackage']='com.hiapk.marketpho' #包名
Start['appActivity']='com.baidu.appsearch.LauncherActivity' #容器
Start['noReset']='True' #是否重新安装app True不重新安装
Start['unicodeKeyboard']='True' #是否禁用手机键盘 True禁用手机输入法
Start['resetKeyboard']='True' #是否启动appium自带键盘 True开始手机输入法
Start['automationName']='uiautomator2' #可选模式 本教程默认 Uiautomator2
Start['newCommandTimeout']='400' #超时时间(s)
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',Start)
driver.implicitly_wait(10) #隐式等待十秒
time.sleep(3)'''模拟器或者手机上已经有了安卓市场,那么启动之后,直接点击左上角的头像图标'''head='com.hiapk.marketpho:id/person_center_btn'driver.find_element_by_id(head).click()#点击头像图标进入个人中心
time.sleep(5)
login='com.hiapk.marketpho:id/please_login'driver.find_element_by_id(login).click()#点击立即登录
time.sleep(5)
为什么要进入到这个页面呢---因为这个页面输入有个toast提示............
如图所示:这个toast提示是在uiautomator上面定位不到的,所以需要稍微处理一下!
然后随便输入一个手机号码和一个密码登记登录:
Elements=driver.find_elements_by_id('com.hiapk.marketpho:id/edt_text')
Elements[0].set_text('17700000000') #因为账号输入和密码输入id相同,所以需要用到elements,之前有介绍过
Elements[1].set_text('123456')
driver.find_element_by_id('com.hiapk.marketpho:id/btn_login').click()
这时手机出现下图所示的toast提示:
之前章节有说过权限弹窗定位的那个方法--用在这里就O了!记得导入这个库
from selenium.webdriver.support import expected_conditions as EC
try:
Element= ("xpath", "//*[@text='网络连接异常,请重试']")
WebDriverWait(driver,30, 0.5).until(EC.presence_of_element_located(Element))print('获取到toast提示:网络连接异常,请重试')except:print('凉了,兄碟')
稍微换一下写法呢:
结果:很舒服---也是可以的--
try:
Element= "//*[@text='网络连接异常,请重试']"WebDriverWait(driver,60, 0.5).until(lambdadriver:driver.find_element_by_xpath(Element))print('获取到toast提示:网络连接异常,请重试')except:print('凉了,兄碟')
备注:toast定位基本上是稳定的,但是偶尔会判断不鸟,属于正常现象,建议把扫描时间0.5s换成0.1s会稳定很多!
WebDriverWait(driver,60, 0.5).until(lambda driver:driver.find_element_by_xpath(Element))
这个是显示等待, 意思就是 等待60s 在这六十秒中,每隔0.5秒扫描一次,是否出现元素 Element
现在把上面的强制等待time.sleep()全部换成显示等待!
from appium importwebdriverfrom selenium.webdriver.support.ui importWebDriverWaitfrom selenium.webdriver.support importexpected_conditions as ECfrom appium.webdriver.common.touch_action import TouchAction #导入Touch Action类
importtime,re,os
Start={}
Start['platformName']='android' #设备型号 android或者ios
Start['platformVersion']='5.1' #安卓设备版本号
Start['deviceName']='192.168.176.101:5555' #安卓设备名称
Start['app']=r'C:\Users\Administrator\Desktop\xuexi\apk\anzhuoshichang_16793302.apk' #设备路径 pc电脑存放apk包的路径#上面的路径不推荐这种写法,但是目前先这么写!后续告诉大家正确的写法
Start['appPackage']='com.hiapk.marketpho' #包名
Start['appActivity']='com.baidu.appsearch.LauncherActivity' #容器
Start['noReset']='True' #是否重新安装app True不重新安装
Start['unicodeKeyboard']='True' #是否禁用手机键盘 True禁用手机输入法
Start['resetKeyboard']='True' #是否启动appium自带键盘 True开始手机输入法
Start['automationName']='uiautomator2' #可选模式 本教程默认 Uiautomator2
Start['newCommandTimeout']='400' #超时时间(s)
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',Start)
driver.implicitly_wait(10) #隐式等待十秒
time.sleep(3)'''模拟器或者手机上已经有了安卓市场,那么启动之后,直接点击左上角的头像图标'''head='com.hiapk.marketpho:id/person_center_btn'driver.find_element_by_id(head).click()#点击头像图标进入个人中心
#time.sleep(5)
login='com.hiapk.marketpho:id/please_login'
#判断十秒内是否获取到登录元素
WebDriverWait(driver,10,0.5).until(lambdadriver:driver.find_element_by_id(login))
driver.find_element_by_id(login).click()#点击立即登录#time.sleep(5)
Elements=driver.find_elements_by_id('com.hiapk.marketpho:id/edt_text')#点击立即登录之后,获取下一页页面的元素
WebDriverWait(driver,10,0.5).until(lambda driver:driver.find_element_by_xpath('//*[@text="手机/邮箱/通行证"]'))
Elements[0].set_text('17700000000') #因为账号输入和密码输入id相同,所以需要用到elements,之前有介绍过
Elements[1].set_text('123456')
driver.find_element_by_id('com.hiapk.marketpho:id/btn_login').click()#因为这个toast提示时间出来的有点晚,所以等待时间稍微加那么一丢丢#try:#Element = ("xpath", "//*[@text='网络连接异常,请重试']")#WebDriverWait(driver,60, 0.5).until(EC.presence_of_element_located(Element))#print('获取到toast提示:网络连接异常,请重试')#except:#print('凉了,兄碟')
try:
Element= "//*[@text='网络连接异常,请重试']"WebDriverWait(driver,60, 0.5).until(lambdadriver:driver.find_element_by_xpath(Element))print('获取到toast提示:网络连接异常,请重试')except:print('凉了,兄碟')
基本上原理都是大同小异....理解了之后,可以写的花里胡哨的!!!!!