元素的id值查询可通过两种方式:
(1).Android SDK的uiautomatorviewer.bat查找
(2).Appium Inspector查找
# -*-coding:utf-8 -*-
# @Author : hudechao
# @Time : 2022/1/20 2:07
from appium import webdriver
desired_caps={}
desired_caps['platformName'] = 'Android'
# 模拟器连接
# desired_caps['platformVersion'] = '7.1.2'
# desired_caps['deviceName'] = '127.0.0.1:62001'
# 真机连接
desired_caps['platformVersion'] = '9'
desired_caps['deviceName'] = 'Redmi'
desired_caps['udid']='f407def6'
# 如果没有安装则进行安装再运行,如果有安装则直接运行考研帮APP
desired_caps['app'] = r'D:\monkeyrunner\kaoyanbang.opdown.com.apk'
desired_caps['appPackage'] = 'com.tal.kaoyan'
desired_caps['appActivity'] = 'com.tal.kaoyan.ui.activity.SplashActivity'
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub",desired_caps)
# 隐士等待
driver.implicitly_wait(5)
driver.find_element_by_id("android:id/button2").click()
driver.find_element_by_id("com.tal.kaoyan:id/tv_skip").click()
元素定位检查
# -*-coding:utf-8 -*-
# @Author : hudechao
# @Time : 2022/1/20 2:07
from appium import webdriver
desired_caps={}
desired_caps['platformName'] = 'Android'
# 模拟器连接
# desired_caps['platformVersion'] = '7.1.2'
# desired_caps['deviceName'] = '127.0.0.1:62001'
# 真机连接
desired_caps['platformVersion'] = '9'
desired_caps['deviceName'] = 'Redmi'
desired_caps['udid']='f407def6'
# 如果没有安装则进行安装再运行,如果有安装则直接运行考研帮APP
desired_caps['app'] = r'D:\monkeyrunner\kaoyanbang.opdown.com.apk'
desired_caps['appPackage'] = 'com.tal.kaoyan'
desired_caps['appActivity'] = 'com.tal.kaoyan.ui.activity.SplashActivity'
# 不重置用户的会话:假如第一次启动过app,再次执行代码则不会出现升级的会话界面
desired_caps['noReset'] = True
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub",desired_caps)
# 隐士等待
driver.implicitly_wait(5)
"""
# 如果在这里元素定位异常,则会抛出异常
cancelButton = driver.find_element_by_id("android:id/button2")
skipButton = driver.find_element_by_id("com.tal.kaoyan:id/tv_skip")
"""
# 改进
try:
ancelButton = driver.find_element_by_id("android:id/button2")
except Exception as e:
print("cancelButton is not exist")
else:
ancelButton.click()
try:
skipButton = driver.find_element_by_id("com.tal.kaoyan:id/tv_skip")
except Exception as e:
print("skipButton is not exist")
else:
skipButton.click()
网友评论