配置相关信息查看 appium.io
初始化:
desired_caps = {} desired_caps["automationName"] = "UiAutomator2" desired_caps["platformName"] = "Android" # 操作系统类型 desired_caps["platformVersion"] = "5.1.1" # 版本5.1.1 desired_caps["deviceName"] = "Android Emulator" # 手机类型 为模拟器 desired_caps["noReset"] = True # 不重置app # aapt dump badging 包路径 在E:Lemon_ClassLemon_appiumandroid-sdk_r24.4.1-windowsandroid-sdk-windowsuild-tools29.0.0>路径下 desired_caps["appPackage"] = "com.tencent.edu" # 使用腾讯课堂app desired_caps["appActivity"] = "com.tencent.edu.module.SplashActivity" # 与appium组成联系,并打开 driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub",desired_caps)
滑动:
# 获取设备高和宽 size = driver.get_window_size() # 从右往左滑动 driver.swipe(size["width"] * 0.9,size["height"] * 0.08,size["width"] * 0.1,size["height"] * 0.08,200) sleep(2) # 从上往下滑动 driver.swipe(size["width"] * 0.5,size["height"] * 0.9,size["width"] * 0.5,size["height"] * 0.1,200)
TouchAction操作:
touch_ele = driver.find_element_by_android_uiautomator('new UiSelector().text("萌姐教你面试的软实力")') loc = touch_ele.location # 获取元素坐标 print(loc) TouchAction(driver).press(x=loc[0],y=loc[1]).perform()
滑动到底部
old = None new = driver.page_source # 获取当前页面页数 while True: if old == new:# 滑动到底部就是页面页数不再发生改变的情况 break else: driver.swipe(size["width"] * 0.5, size["height"] * 0.9, size["width"] * 0.5, size["height"] * 0.1, 200) sleep(2) old = new new = driver.page_source
滑动到指定元素:
old = None new = driver.page_source # 获取当前页面页数 ele = "指定元素" while True: if old == new:# 滑动到底部就是页面页数不再发生改变的情况 break else: if driver.page_source.find(ele) != -1: print("找到了元素/滑动到了指定元素的当前页面") break else: driver.swipe(size["width"] * 0.5, size["height"] * 0.9, size["width"] * 0.5, size["height"] * 0.1, 200) sleep(2) old = new new = driver.page_source
Toast抓取:
try: xpath = '//*[contains(@text,"toast提示框文本内容,部分或者全部")]' # 只能使用presence_of_element_located 使用visibility_of_element_located会报错 WebDriverWait(driver,30,0.01).until(EC.presence_of_element_located((MobileBy.XPATH,xpath)) print("成功捕捉") except Exception as e: print("未成功") print(e) else: print("找到对应toast了")
H5界面操作
# 进入有页面(H5)的界面(context),也就是class = android.webkit.WebView # 获取所有context. sleep(5) cons = driver.contexts print(cons) # 根据context名字,切换到WebView # 参考 www.lemfix/topics/317 driver.switch_to.context('WEBVIEW_com.lemon.lemonban') # web自动化操作 # 1.使用 uc-devtool 百度云有安装包 需要移动端界面处于h5界面(界面显示chrome版本信息方便下载 chromedriver) 在inspect点击后 # 2.翻墙 chrome://inspect # 3.driver.page_source得到html页面源码 # 4.chromedriver存放,仅限安卓 desired_caps["chromedriverExecutable"] = 'driver存放目录' # 一般在开头声明 # 5.返回app界面 driver.switch_to.context(None)
x5内核应用
# 微信x5界面 # www.lemfix.com/topics/317 开启微信调试模式教程 # 支持X5内核应用自动化配置 appium.io中的Android_only desired_caps["recreateChromeDriverSessions"] = True # 确认当前web页面所在进程 """ cmd后 adb shell dumpsys activity top findstr ACTIVITY 获取当前进程中 app所在页面的进程序号 adb shell ps app所在页面的进程序号 获取末尾name下的com.tencent.mm:appbrand0(页面所在进程名称) """ desired_caps["chromeOptions"] = {"androidProcess":"com.tencent.mm:appbrand0"} desired_caps["browserName"] = "" # 浏览器名字 Safari chrome等 如果是在APP内 则置空处理 # 参考www.cnblogs.com/yyoba/p/9455519.html
adb命令通过坐标点击
os.system("adb shell input tap 350 736")
窗口过多可以使用窗口切换配合find查找使用
# app内窗口切换 hs = driver.window_handles for handle in hs: driver.switch_to_window(handle) print(handle) sleep(3) if driver.page_source.find("文本信息")!= -1: break