from appium import webdriver from appium.webdriver.common.appiumby import AppiumBy from appium.webdriver.extensions.android.nativekey import AndroidKey from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from appium.webdriver.common.touch_action import TouchAction import time ''' 通过adb命令查看当前进程 adb shell dumpsys activity top|findstr ACTIVITY adb shell ps pid 放在实例化Remote配置信息中 "chromeOptions": {"androidProcess": "com.tencent.mm:appbrand0"} { "platformName": "Android", "platformVersion": "10", "deviceName": "slkMobile", "appPackage": "com.tencent.mm", "appActivity": ".ui.LauncherUI", "unicodeKeyboard": true, "resetKeyboard": true, "noReset": true, "newCommandTimeout": 6000, "automationName": "UiAutomator2" , "chromeOptions": {"androidProcess": "com.tencent.mm:appbrand0"} } ''' class minitab(): def __init__(self): print("---init启动app-") desired_caps = { 'platformName': 'Android', # 被测手机是安卓 'platformVersion': '10', # 手机安卓版本 'deviceName': 'slkMobile', # 设备名,安卓手机可以随意填写 'appPackage': 'com.tencent.mm', # 启动APP Package名称 'appActivity': '.ui.LauncherUI', # 启动Activity名称 'unicodeKeyboard': True, # 使用自带输入法,输入中文时填True 'resetKeyboard': True, # 执行完程序恢复原来输入法 'noReset': True, # 不要重置App 'newCommandTimeout': 6000, 'automationName': 'UiAutomator2', 'chromeOptions': {'androidProcess': 'com.tencent.mm:appbrand0'} } # 连接Appium Server,初始化自动化环境 self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) time.sleep(10) def swipe_win(self): print("---下滑屏幕") # 获取屏幕宽度 width = self.driver.get_window_size()['width'] # 获取屏幕高度 height = self.driver.get_window_size()['height'] self.driver.swipe(width * 0.5, height * 0.1, width * 0.5, height * 0.9, 1000) def click_mini_title(self): print("---点击搜图大师title") try: DCD = '//*[@text="搜图大师"]' # WebDriverWait(self.driver, 100).until(EC.visibility_of_element_located((By.XPATH, DCD))) # self.driver.find_element_by_xpath(DCD).click() self.driver.find_element(By.XPATH, "//*[@text='搜图大师']").click() time.sleep(8) print("---小程序8s广告结束") except: pass def switch_now(self): print("---切换上下文进入小程序") contexts = self.driver.contexts self.driver.switch_to.context(contexts[-1]) print(contexts) # 当前会有很多窗口句柄,需要遍历获取元素,切换到当前的windowHandle def click_elem_contexts(self): all_contexts = self.driver.contexts print(all_contexts) my_path='/hierarchy/node/node/node/node/node/node/node/node/node/node/node/node/node/node/node[' \ '@resource-id="com.tencent.mm:id/uv"] ' self.driver.find_element(By.XPATH, my_path)[0].click() # self.driver.find_element(By.XPATH, "//*[@text='视频去水印']").click() # driver.switch_to.context(driver.contexts[-1]) # for handle in all_contexts: # try: # self.driver.switch_to.context(handle) # self.driver.find_element(By.XPATH, "//*[@text='视频去水印']").click() # except Exception: # print("---定位失败") # print(Exception) def operate(self): print("---start") self.swipe_win() self.click_mini_title() self.switch_now() self.click_elem_contexts() if __name__ == '__main__': minitab().operate()
demo appium
最新推荐文章于 2024-08-24 11:00:49 发布