在介绍滑动查找元素之前, 请先容许我插上一嘴 -- 之前章节有更新过 Uiobject uicollection uiselector 这里还有一个新东西叫做 uiscrollable 这个 class 它里面是封装了所有的滑动处理的相关操作!
name 这个叫做 uiscrollable 的类里面有那些东西了:
Scrollintoview----- 获取某个可滑动区域, 然后滑动这个区域里面的元素查找我们所需要的元素
- # 后面的 text 查找条件可以用组合查找也可以用 textcontains 关键之查找 text, 一般查找条件使用 text..... 因为一般可滑动的区域 id 和 class 都是一样的...
- driver.find_element_by_android_uiautomator\
- ('new UiScrollable(new UiSelector().resourceId(" 被滑动区域最后一层的 id")).scrollIntoView(new UiSelector().text(" 我们需要查找的元素 text"))')
代码:
- from appium import webdriver
- from selenium.webdriver.support.ui import WebDriverWait
- from selenium.webdriver.support import expected_conditions as EC
- from appium.webdriver.common.touch_action import TouchAction #导入 Touch Action 类
- import time,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)
- driver.find_elements_by_id('com.hiapk.marketpho:id/title')[0].click() #点击首页 必备
- WebDriverWait(driver,30,0.1).until(lambda driver:driver.find_element_by_id("com.hiapk.marketpho:id/libui_title_back_btn"))
- driver.find_element_by_android_uiautomator('new UiScrollable(new UiSelector().resourceId("com.hiapk.marketpho:id/listview")).scrollIntoView(new UiSelector().textContains(" 滴滴车主 "))').click()
如果跟着操作了 -- 是不是感觉上面说的那个 [被滑动区域] 很难找....
So.. 从最上级开始看 ---- 有下级内容的就点一下 , 看下元素的 scroll 属性是不是为 true
如下图所示: 这个看上去是不是好像是我们想要的区域 --- 但是仔细一看 ---scroll 是 false-- 所以这个也不是, 那么继续往下找
再继续往下找, 结果发现下一个属性就是为 true 的 -- 那么这个就是我们的想要的....
备注: 这种是官方给出的滑动查找元素的 api.... 我记得貌似 Uiscrollable 这个 class 里面还有很多花里胡哨的滑动的方法... 我之前有试过... 结果发现除了 这个 scrollIntoview 之外, 其他的基本报错.. 因为其他的 api 包括这个 scrollIntoview 我全部都没有用到.. 所以没有去研究..
那么问题来了, 我这边滑动查找元素是用的什么方法呢
...
记得上一章有介绍过 TouchAction 这个很牛皮的方法是吧.... 我这边用的各个方位查找元素都是用的这个.. 并且对比了一下 api 貌似他的也是调用的 TouchAction 这个方法实现的..
So..
支持各个方位滑动查找元素.. 唯一的缺点就是貌似稍微慢了那么一丢丢, 只是一丢丢, 实际上和 scrollIntoview 查找速度差不多,, 建议把 wait 的时间稍微调的长一点.. 时间越短越容易报错...
代码:
- from appium import webdriver
- from selenium.webdriver.support.ui import WebDriverWait
- from selenium.webdriver.support import expected_conditions as EC
- from appium.webdriver.common.touch_action import TouchAction #导入 Touch Action 类
- import time,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)
- driver.find_elements_by_id('com.hiapk.marketpho:id/title')[0].click() #点击首页 必备
- WebDriverWait(driver,30,0.1).until(lambda driver:driver.find_element_by_id("com.hiapk.marketpho:id/libui_title_back_btn"))
- # driver.find_element_by_android_uiautomator('new UiScrollable(new UiSelector().resourceId("com.hiapk.marketpho:id/listview")).scrollIntoView(new UiSelector().textContains(" 滴滴车主 "))').click()
- def scroll_my(x1, y1, x2, y2, element, type, timeOut):
- '''无限各方为滑动加载获取指定元素'''
- timeStart = time.strftime('%d%H%M%S', time.localtime())
- while 1:
- try:
- if type == 'id':
- driver.find_element_by_id(element)
- if type == 'xpath':
- driver.find_element_by_xpath(element) #另外这里还可以写其他的元素定位方式在这里 -- 建议把每个定位方式独立的封装成一个方法..
用到定位方式就调用哪个方法 --- 因为判断多了, 代码执行就会耗时那么一丢丢..
- break
- except:
- action = TouchAction(driver)
- action.press(x=x1, y=y1).wait(ms=500).move_to(x=x2, y=y2).release()
- action.perform()
- timeOver = time.strftime('%d%H%M%S', time.localtime())
- if int(timeOver) - int(timeStart)>= int(timeOut):
- break
- else:
- continue
- if type == 'id':
- return driver.find_element_by_id(element)
- if type == 'xpath':
- return driver.find_element_by_xpath(element)
- scroll_my(0.5,0.9,0.5,0.4,'//*[@text=" 滴滴车主 "]','xpath',300).click()
那么问题来了 ,TouchAction 还有没有其他的骚操作呢?
来源: https://www.cnblogs.com/pythontest/p/9527549.html