swpie()类 滑动接口: swipe(起始X,起始Y,结束X,结束Y) 结束X-起始X:X轴滑动的距离 结束Y-起始Y:Y轴滑动的距离 因为不同的手机,屏幕大小是不一样的,所以兼容性操作如下:
#weight、height size = driver.get_window_size() startx_x = size['width'] * 0.9 startx_y = size['height'] * 0.5 endx_x = size['width'] * 0.1 endx_y = size['height'] * 0.5 #从右向左划 driver.swipe(startx_x,startx_y,endx_x,endx_y,duration=200) #从左向右滑 driver.swipe(endx_x,endx_y,startx_x,startx_y,duration=200) starty_x = size['wight'] * 0.5 starty_y = size['height'] * 0.9 endy_x = size['wight'] * 0.5 endy_y = size['height'] * 0.1 #从下往上, driver.swipe(starty_x,starty_y,endy_x,endy_y,duration=200) # 从上往下 driver.swipe(endy_x,endy_y,starty_x,starty_y,duration=200)TouchAction类 将一系列的动作放在一个链条中,然后将该链条传递给服务器,服务器接收该链条后,解析所有动作,然后逐个执行 短按(press) 长按(LongPress) 点击(tap) 移动到(move_to) 等待(wait) 释放(release) 执行(perform) 取消(cancel) 示例
ele = driver.find_element_by_id("xxx") #元素的大小 size = ele.size #均分的步长(高和宽一样) step = size['width']/6 #元素的起点坐标 左上角 ori = ele.location #原点 point1 = (ori['x']+step,ori['y']+step) point2 = (point1[0]+step*2,point1[1]) #X轴增加了2个step point3 = (point1[0]+step*4,point1[1]) point4 = (point2[0],point2[1]+step*2) point5 = (point2[0],point2[1]+step*4) TouchAction(driver).press(x=point1[0],y=point1[1]).wait(200)\ .move_to(x=point2[0],y=point2[1]).wait(200)\ .move_to(x=point3[0],y=point3[1]).wait(200)\ .move_to(x=point4[0],y=point4[1]).wait(200)\ .move_to(x=point5[0],y=point5[1]).wait(200)\ .release()\ .perform()要获取到toast,要满足下面两个要求: 1、appium server 版本1.6.3+才支持toast获取 2、代码中必须指定automationName为:UiAutomator2 3、UIautomator2只支持安卓版本5.0+ 4、要求安装jdk1.8 64位以上,配置环境变量JAVA_HOME和path 配置toast注意事项:
实现
#1、xpath表达式,文本匹配 loc = '//*[contains(@text,"{}")]'.format(str) #2、等待的时候,要用元素存在的条件,不能用元素可见的属性 try: WebDriverWait(self.driver,10,0.01).until(EC.presence_of_element_located((MobileBy.XPATH,loc))) return self.driver.find_element_by_xpath(loc).text except: my_logger.exception("没有找到匹配的toast!!!") raise在driverWait方法中,用presence_of_element_located方法,不要用visibility_of_element_located,visibility_of_element_located对toast可见不支持,命令会报错无法执行
基于UiAutomator+Chromedriver native部分走Chromedriver,webview部分走UiAutomator,二者结合,要求: 1、Android 4.4+ 2、webview必须为debug版本
debug模式是怎么来的: 1、debug打包的时候,需要开户webview的debug属性WebView.setWebContentsDebuggingEnabled(true),这个是开发需要加的 2、模拟器的contents中有webview,但有些手机没有,官方的答案是:需要root权限,然后再获取(有很多工具可以来 root,比如KingRoot、一键root、360一键root等。如果你安装了QQ电脑管家,可以在“电脑管家-工具箱-其他”列表里面看到KingRoot)
获取webdriver页面的三种方式 1、使用driver.page_source获取html页面 2、找开发人员要源文件 3、uc-devtools uc-devtools下载链接
H5 页面调试小帮手-uc-devtools 开发者工具的使用
上下文切换 可用的上下文(contexts) 列出所有可用上下文 driver.context driver.window_handles() 当前上下文(context),列出当前的上下文 driver.current_context() 切换至默认上下文 driver.switch_to.context(None) 当前的Activity:获取当前的activity,仅支持Android driver.current_activity() 当前包名(package):获取当前的package,仅支持Android driver.current_package() 混合型自动化项目:
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 from appium.webdriver.common.touch_action import TouchAction import pytest import time desired_caps = { 'automationName':'UiAutomator2', '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, # 输入中文时要加,要不然输入不了中文 'noReset':True #是否重置 --不重置 } #连接appium server,appium desktop要启动,有监听端口 #要将服务器参数发送过去 driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps) loc = 'new UiSelector().text("全程班")' WebDriverWait(driver,20).until(EC.visibility_of_element_located((MobileBy.ANDROID_UIAUTOMATOR,loc))) driver.find_element_by_android_uiautomator(loc).click() #等待WebView元素出现--html WebDriverWait(driver,20).until(EC.visibility_of_element_located((MobileBy.CLASS_NAME,"android.webkit.WebView"))) time.sleep(2) #前提:我们可以识别到webview,需要开启app的webview debug属性 #content #1、先列出所有的context cons = driver.contexts #列表 #2、切换至webview,需要确保chromedriver与WebView的版本匹配,chromedriver需要放在正确的目录下(运行时有错误提示) driver.switch_to.context(cons[-1]) #3、切换之后,当前操作对象是html页面 #等待元素可见 WebDriverWait(driver,20).until(EC.visibility_of_element_located((MobileBy.XPATH,"xxxx"))) driver.find_element_by_xpath("xxxx")当运行时报如下错:当自己的chromedriver版本满足条件时,还报错,就说明是appium的问题,需要换appium版本