本文是基于python语言在android上实操的,仅记录(忽略排版~~~)
会不时更新的:
1 from appium import webdriver 2 from selenium.webdriver.common.touch_actions import TouchActions #导入TouchActions类 3 import time 4 5 caps = {} 6 caps["platformName"] = "Android" 7 caps["platformVersion"] = "8.0.0" 8 caps["deviceName"] = "Phone" 9 caps["appPackage"] = "com.android.settings" 10 caps["appActivity"] = ".Settings" 11 12 driver = webdriver.Remote("http://localhost:4723/wd/hub", caps) 13 14 time.sleep(1) 15 driver.find_element_by_xpath("//android.widget.TextView[@text='Network & Internet']").click() 16 print(driver.available_ime_engines) #返回安卓设备可用的输入法 17 driver.open_notifications() #打系统通知栏 18 time.sleep(2) 19 driver.press_keycode(4) #点击返回键关闭通知栏 20 driver.save_screenshot("new.png") #截取当前屏幕并命名new.png,然后保存到电脑脚本当前目录 21 22 '''driver.find_element_by_xpath("//android.widget.TextView[@text='Display']").click() #使用xpath方法定位 23 m = driver.find_element_by_xpath("//android.widget.TextView[@text='Adaptive brightness']") 24 print(m.get_attribute("className")) #获取元素m的className值 ''' 25 ''' 26 e = driver.find_element_by_android_uiautomator('new UiSelector().className("android.widget.SeekBar")') #使用uiautomator搭配class属性方法定位 控制条 27 ex = e.location.get('x') #获取元素初始横坐标 28 ey = e.location.get('y') #获取元素初始纵坐标 29 #driver.tap([(ex,ey)],500) #用tap方法点击拖动按钮的最左侧起始位置 30 #driver.tap([(ex+400,ey)],500) #用tap方法横向点击某按钮,ex+400,ey不变 31 driver.swipe(ex,ey,ex+400,ey,500) #用swipe方法横向拖动某按钮, ey纵坐标不变 32 time.sleep(1) 33 driver.press_keycode(4) #按导航返回键''' 34 35 #driver.find_element_by_xpath("//android.widget.TextView[@text='Network & Internet']") 36 37 '''k = driver.find_element_by_xpath("//android.widget.TextView[@text='Network & Internet']") #定位元素Network & Internet,通过xpath方式 38 kx = k.location.get('x') #获取元素的x坐标 39 ky = k.location.get('y') #获取元素的y坐标 40 driver.tap([(kx,ky)],5000) #长按元素坐标5s 41 time.sleep(2) 42 driver.install_app('F:\soft\weixin.apk') #安装app,后接apk路径 43 #driver.remove_app("com.tencent.mm") #删除app(包名) 44 driver.press_keycode('4') #按返回键 45 #driver.keyevent('3') #按home键 46 time.sleep(2)''' 47 48 ''' 49 driver.find_element_by_accessibility_id("Search settings").click() #定位并点击click() 50 #driver.find_element_by_id("android:id/search_src_text").send_keys('wifi') #定位到搜索输入栏,用.send_keys()方法模拟输入文本 51 #driver.find_element_by_id("android:id/search_src_text") #定位到搜索输入栏 52 driver.press_keycode(42) #模拟点击键盘上的n 53 driver.press_keycode(34) #模拟点击键盘上的f 54 driver.press_keycode(31) #模拟点击键盘上的c 55 time.sleep(1) 56 driver.find_element_by_accessibility_id("Clear query").click() #清除文本,用.clear()方法来清除输入文本 通过id方式定位 57 driver.find_element_by_accessibility_id("Navigate up").click() #点击返回键''' 58 ''' 59 k = driver.find_element_by_xpath("//android.widget.TextView[@text='Network & Internet']") #定位元素并附值给k 60 print(k.size) #获取K元素的size,包括height, width 61 print(driver.get_window_size()) #打印屏幕分辨率 62 ''' 63 '''time.sleep(2) #强制等待2s 64 x = driver.get_window_size()['width'] #获取屏幕的宽度 65 y = driver.get_window_size()['height'] #获取屏幕的高度 66 driver.swipe(1/2*x, 5/7*y, 1/2*x, 2/7*y, 1000) #从下往上滑动屏幕(5/7->2/7) 67 68 time.sleep(2) 69 70 driver.close_app() #关闭app 71 time.sleep(2) 72 driver.launch_app() #启动app 73 time.sleep(2) 74 driver.get_screenshot_as_file('G:\Kemi\pic.png') #截取当前屏幕并命名pic.png,然后保存到电脑G:Kemi下 75 driver.save_screenshot("newContact.png") #截取当前屏幕并命名pic.png,然后保存到电脑当前目录下''' 76 77 time.sleep(2) 78 79 #driver.is_app_installed("com.android.settings") #检查app是否安装 80 #driver.find_element_by_xpath("//android.widget.TextView[@text='Brightness level']").is_displayed() # 定位元素并判断是否显示 81 #driver.find_element_by_xpath("//android.widget.TextView[@text='Brightness level']").is_enabled() #判断是否可用 82 83 #self.driver.find_element_by_android_uiautomator('new UiSelector().text("Long")').click() #使用uiautomator搭配text属性方法定位 弹框选项 84 85 x = driver.get_window_size()['width'] 86 y = driver.get_window_size()['height'] 87 driver.swipe(1/2*x, 6/7*y, 1/2*x, 1/7*y, 1000) #从下往上滑动屏幕 88 time.sleep(1) 89 driver.find_element_by_xpath("//android.widget.TextView[@text='Support']").click() 90 try: #使用try-except判断是否有弹窗,没有就pass跳过势将执行后面的操作 91 driver.find_element_by_id("com.sonymobile.support:id/eula_agree").click() 92 except Exception as e: 93 pass 94 try: 95 driver.find_element_by_id("com.android.packageinstaller:id/permission_allow_button").click() 96 except Exception as e: 97 pass 98 driver.find_element_by_xpath("//android.widget.ImageView[@resource-id='com.sonymobile.support:id/icon'][@instance='4']").click() #使用xpath下class、resource-id、instance联合定位 99 #driver.find_element_by_xpath("//android.widget.ImageView[@instance='4']").click() #效果同上句,用xpath下class及instance联合定位