在android目录下找到uiautomatorviewer.bat,然后双击,页面的第二个按钮连接设备
D:Program Filesandroid-sdk-windows oolsuiautomatorviewer.bat
from appium import webdriver caps = {} caps["appPackage"] = "com.tencent.mobileqq" # caps["appPackage"] = "com.android.settings" caps["appActivity"] = "com.tencent.mobileqq.activity.SplashActivity" # caps["appActivity"] = ".Settings" caps["platformName"] = "Android" caps["platformVersion"] = '4.4.2' caps["deviceName"] = '127.0.0.1:62001' caps["unicodeKeyboard"] = True caps["resetKeyboard"] = True caps["noReset"] = True driver = webdriver.Remote("http://localhost:4723/wd/hub", caps) #获取手机大小 {'width': 720, 'height': 1280} size = driver.get_window_size() print(size) #如果安装了返回True print(driver.is_app_installed("com.tencent.mobileqq")) #关闭app driver.close_app() import time time.sleep(3) #启动app,启动是初始化的app driver.launch_app() #1、除默认的初始化app之外 还可以在启动其他app driver.close_app() driver.start_activity('com.android.settings', '.Settings') #2、他可以跳过某些场景,达到某个页面 # element = driver.find_element_by_android_uiautomator('new UiSelector().text("新用户")') # element.click() print(driver.current_activity) ele = driver.start_activity('com.tencent.mobileqq', '.activity.RegisterPhoneNumActivity') print(driver.current_activity)
#如果send_keys 报错的话使用set_value
driver.set_value(ele, '123456')
# 返回当前session中的app类型
driver.current_context
# 返回当前app的类型
# WEBVIEW 或 WEBVIEW
# 底层实际调用current_context
driver.context
# 获取app所有的类型
# 有WEBVIEW的则以list的形式展示两个
driver.contexts
# app类型切换 参数接收app类型
# appium对selenium的switch_to的扩展
# 增加了MobileSwitchTo,继承了selenium的switch_to
driver.switch_to.context('context')
element = driver.find_element_by_id('com.android.browser:id/url') element.send_keys('http://ui.imdsx.cn/uitester/') time.sleep(5) # key code Android按键码 driver.press_keycode(66) time.sleep(2) # 打印当前的app类型 print(driver.current_context) # 从原生切换到H5 driver.switch_to.context(driver.contexts[1]) # 定位id为i1的元素 driver.find_element_by_css_selector('#i1').send_keys('11111') # 打印切换玩的app类型 print(driver.current_context)