04 APP 自动化- Appium toast 元素定位&列表滑动

发布于:2025-06-05 ⋅ 阅读:(24) ⋅ 点赞:(0)

文章目录

      • 一、toast 元素的定位
      • 二、滑屏操作

一、toast 元素的定位

toast 元素就是简易的消息提示框,toast 显示窗口显示的时间有限,一般3秒左右

# -*- coding=utf-8 -*-
from time import sleep
from appium import webdriver
from appium.options.android import UiAutomator2Options
from appium.webdriver.common.appiumby import AppiumBy
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as Ec

# 设置操作终端的配置参数
desired_caps = dict(
    platformName='Android', # 指定操作系统
    platformVersion='12',# 指定操作系统版本
    automationName='Uiautomator2',# 默认框架
    deviceName='127.0.0.1:62001',# 指定设备名称
    appPackage='com.tal.kaoyan',# 被操作的应用程序包名
    appActivity='com.tal.kaoyan.ui.activity.SplashActivity',# 启动页面
    noReset='true',# true--不重置  false--重置
    app='F:\Pycharm\AppAuto\kaoyan_v4.5.3.apk'  # apk文件所在路径
)
# 发送命令给 appium server
driver = webdriver.Remote('http://127.0.0.1:4723', options=UiAutomator2Options().load_capabilities(desired_caps))
#-------------手机号密码登录----------------
phone_loc = (AppiumBy.ID, 'com.tal.kaoyan:id/loginEmailEdittext')
WebDriverWait(driver, 5).until(Ec.presence_of_element_located(phone_loc))
phone_input = driver.find_element(*phone_loc)
# 清空手机号输入框
phone_input.clear()
# 输入手机号
phone_input.send_keys('15956423841')

password_input = driver.find_element(AppiumBy.ID, 'com.tal.kaoyan:id/rtlLoginLayout')
# 清空密码输入框
# password_input.clear()
# clear()方法无效,可以模拟键盘操作手动删除文本框内容
password_input.click()
driver.press_keycode(67).press_keycode(67).press_keycode(67).press_keycode(67).press_keycode(67).press_keycode(67).press_keycode(67).press_keycode(67).press_keycode(67)

# 输入密码
# password_input.send_keys('121335')
# send_keys() 无效,模拟键盘输入
driver.press_keycode(8).press_keycode(14).press_keycode(14).press_keycode(8).press_keycode(7)

protocol_agree_check = driver.find_element(AppiumBy.ID, 'com.tal.kaoyan:id/loginTreatyCheckboxPassword')
checked = protocol_agree_check.get_attribute("checked")
# 未勾选同意协议,则进行点击同意协议
if checked == 'false':
    protocol_agree_check.click()

login_btn = driver.find_element(AppiumBy.ID, 'com.tal.kaoyan:id/loginLoginBtn')
# 点击登录
login_btn.click()

toast_loc = (AppiumBy.XPATH, "//*[contains(@text,'帐号或密码错误')]")
# 显示等待
WebDriverWait(driver, 15,0.01).until(Ec.presence_of_element_located(toast_loc))
toast_text = driver.find_element(*toast_loc).text
print("获取 toast 元素的文本内容:",toast_text)

二、滑屏操作

  • 同一个水平位置左滑:
    • 开始位置与结束位置的坐标特点:
      • Y坐标相同
      • x坐标从大到小
  • 同一个水平位置右滑:
    • 开始位置与结束位置的坐标特点:
      • Y坐标相同
      • x坐标从小到大
  • 上滑:
    • 开始位置与结束位置的坐标特点:
      • X坐标相同
      • Y坐标从大到小
  • 下滑:
    • 开始位置与结束位置的坐标特点:
      • X坐标相同
      • Y坐标从小到大

# -*- coding=utf-8 -*-
from appium import webdriver
from appium.options.android import UiAutomator2Options

# 设置操作终端的配置参数
desired_caps = dict(
    platformName='Android', # 指定操作系统
    platformVersion='12',# 指定操作系统版本
    automationName='Uiautomator2',# 默认框架
    deviceName='127.0.0.1:62001',# 指定设备名称
    appPackage='com.tal.kaoyan',# 被操作的应用程序包名
    appActivity='com.tal.kaoyan.ui.activity.SplashActivity',# 启动页面
    noReset='true',# true--不重置  false--重置
    app='F:\Pycharm\AppAuto\kaoyan_v4.5.3.apk'  # apk文件所在路径
)
# 发送命令给 appium server
driver = webdriver.Remote('http://127.0.0.1:4723', options=UiAutomator2Options().load_capabilities(desired_caps))
#实现滑屏
# 获取整个app屏幕的大小
size = driver.get_window_size()
x = size["width"]
y = size["height"]
# 左滑2次
for i in range(0,2):
  driver.swipe(start_x=x*0.9,end_x=x*0.2,start_y=y*0.9,end_y=y*0.9,duration=1000)
print("实现左滑两次")
# 右滑2次
for i in range(0,2):
  driver.swipe(start_x=x*0.2,end_x=x*0.9,start_y=y*0.9,end_y=y*0.9,duration=1000)
print("实现右滑两次")

网站公告

今日签到

点亮在社区的每一天
去签到