appium自动化工具使用--模拟登陆考研帮app并实现滑动操作
一、目标
-
实现登陆考研帮app
-
点击社区按钮,并对里面的信息进行滑动操作
二、工具
-
一台手机或者安卓模拟器,进入开发者模式,进行usb调试
-
Android SDK中的uiautomatorviewer,用于定位app中的控件或元素
-
Appium服务端,开启服务器,并复制出里面配置好的考研帮app的desired_capabilities
三、代码实现
# coding:utf-8 import time from appium import webdriver from selenium.webdriver.support.ui import WebDriverWait as WAIT from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By #appium中用于开启并控制考研帮app的desired_capabilities cap = { "platformName": "Android", "platformVersion": "7.1.2", "deviceName": "127.0.0.1:62001", "appPackage": "com.tal.kaoyan", "appActivity": "com.tal.kaoyan.ui.activity.SplashActivity", "noReset": True } #连接appium服务器 driver = webdriver.Remote('http://localhost:4723/wd/hub',cap) #非首次登录 try: tv_skip = WAIT(driver,3).until(lambda x:x.find_element_by_id('com.tal.kaoyan:id/tv_skip')) tv_skip.click() #首次登录 except: # 用户个人信息保护政策 try: #使用以下两种等待条件都可以 protection_policy = WAIT(driver, 5).until(EC.presence_of_element_located((By.XPATH,"//android.widget.TextView[@resource-id='com.tal.kaoyan:id/tip_commit']"))) #protection_policy = WAIT(driver,5).until(lambda x:x.find_element_by_xpath("//android.widget.TextView[@resource-id='com.tal.kaoyan:id/tip_commit']")) protection_policy.click() except: pass # 我知道了 try: i_know = WAIT(driver,10).until(lambda x:x.find_element_by_id("com.tal.kaoyan:id/tv_ok")) i_know.click() except: pass #立即体验 try: experience_now = WAIT(driver,50).until(lambda x:x.find_element_by_xpath("//android.widget.TextView[@resource-id='com.tal.kaoyan:id/activity_splash_guidfinish']")) experience_now.click() except: pass #切换到密码登陆 try: login_by_passwd = WAIT(driver,5).until(lambda x:x.find_element_by_xpath("//android.widget.TextView[@resource-id='com.tal.kaoyan:id/login_code_touname']")) login_by_passwd.click() except: pass #输入账号密码并勾选用户协议,登陆 try: username = WAIT(driver,5).until(lambda x:x.find_element_by_xpath("//android.widget.EditText[@resource-id='com.tal.kaoyan:id/login_email_edittext']")) username.send_keys('账号或用户名') #输入用户名 driver.find_element_by_xpath("//android.widget.EditText[@resource-id='com.tal.kaoyan:id/login_password_edittext']").send_keys('密码') #输入密码 driver.find_element_by_xpath("//android.widget.CheckBox[@resource-id='com.tal.kaoyan:id/login_treaty_checkbox_password']").click() #勾选用户协议 driver.find_element_by_xpath("//android.widget.TextView[@resource-id='com.tal.kaoyan:id/login_login_btn']").click() except: pass #跳过 try: skip = WAIT(driver,5).until(lambda x:x.find_element_by_xpath("//android.widget.TextView[@resource-id='com.tal.kaoyan:id/kylogin_perfect_tag_jump_button']")) skip.click() except: pass #点击社区 try: community = WAIT(driver,10).until(lambda x:x.find_element_by_id("com.tal.kaoyan:id/mainactivity_button_forum")) community.click() except: pass time.sleep(3) #向上滑动 #从中间向下3/4位置向上滑动到1/4位置 window_size = driver.get_window_size() x = int(window_size['width']*0.5) start_y = int(window_size['height']*0.75) end_y = int(window_size['height']*0.25) #模拟滑动5次,每次间隔2秒 for i in range(5): driver.swipe(x,start_y,x,end_y) time.sleep(2)