Appium中的api(二)

目录

元素定位操作api

1--通过id定位api

2--通过class获取定位元素

3--通过xpath表达式定位元素

4.完整代码

解释

效果


元素定位操作api

1--通过id定位api

注:driver.find_element是获取单个元素

# 通过id获取

mySearchId ="com.android.settings:id/search_action_bar"

searchElement = self.driver.find_element(AppiumBy.ID,mySearchId)

2--通过class获取定位元素

注:driver.find_elements是获取一组元素集合,如果没有则会报错

print("通过class获取一组元素")
# 通过class获取一组TextView
myElements= self.driver.find_elements(by=AppiumBy.CLASS_NAME, value=myClassName)

#循环遍历输出

for myElementC in myElements:

print(myElementC.text)

3--通过xpath表达式定位元素

通过xpath获取元素

myXpath = "//android.widget.TextView[@text='电池']"

myElementByXpaths = self.driver.find_elements(by=AppiumBy.XPATH, value=myXpath)

4.完整代码

python 复制代码
import time
import unittest
from appium import webdriver
from appium.options.android import UiAutomator2Options
from appium.webdriver.common.appiumby import AppiumBy

capabilities = dict(
    platformName='Android', # 名字,这个这就是这个
    automationName='uiautomator2', # 这个是使用的驱动
    deviceName='Android',  # 这个是设备名字可以随意
    appPackage='com.android.settings',  # 这个是你要启动的包名
    appActivity='.Settings',    # 这个是你要启动的活动单元名字
    language='en',
    locale='US',
    udid="127.0.0.1:21503"  # 这个是要连接的设备的ip和端口号    我用的是逍遥模拟器  端口号为 21503
)

appium_server_url = 'http://localhost:4723'

class MyElementUiTest(unittest.TestCase):

    # 前置处理
    def setUp(self) -> None:
        self.driver = webdriver.Remote(appium_server_url, options=UiAutomator2Options().load_capabilities(capabilities))


    # 后置处理  关闭连接会话session
    def tearDown(self) -> None:
        self.driver.quit()


    # test
    def test_find_by_id(self):
        # 获取当前界面的信息
        myPackageInfo =  self.driver.current_package;
        print(myPackageInfo)
        # 获取当前页面的包名
        myActivityInfo = self.driver.current_activity
        print(myActivityInfo)

        # 通过id获取元素
        mySearchId = "com.android.settings:id/search_action_bar"
        myElement = self.driver.find_element(by=AppiumBy.ID, value=mySearchId)
        time.sleep(2)


        print("通过class获取单个元素")
        # 通过class获取元素
        myClassName = "android.widget.TextView"
        myElementByClassName = self.driver.find_element(by=AppiumBy.CLASS_NAME, value=myClassName)
        # 输出元素的信息
        print(myElementByClassName.text)

        # 获取text属性值
        print(myElementByClassName.get_attribute("text"))
        # 获取class属性值
        print(myElementByClassName.get_attribute("class"))
        # 获取package属性值
        print(myElementByClassName.get_attribute("package"))
        # 获取content-desc属性值
        print(myElementByClassName.get_attribute("content-desc"))








        print("通过class获取一组元素")
        # 通过class获取一组TextView
        myElements = self.driver.find_elements(by=AppiumBy.CLASS_NAME, value=myClassName)

        #循环遍历输出
        for myElementC in myElements:
            print(myElementC.text)


        print("通过xpath获取元素")
        # 通过xpath获取元素
        myXpath = "//android.widget.TextView[@text='电池']"
        myElementByXpaths = self.driver.find_elements(by=AppiumBy.XPATH, value=myXpath)
        print(myElementByXpaths)
        time.sleep(5)
解释
  1. 导入必要的模块

    • time: 提供了各种时间相关的函数
    • unittest: Python内置的单元测试框架。
    • webdriverUiAutomator2Optionsappium 模块中导入,用于初始化WebDriver会话。
    • AppiumBy 提供了查找元素的方式。
  2. 设置测试能力(Capabilities)

    • platformName:指定目标平台,这里是Android。
    • automationName:指定使用的自动化引擎,这里是UiAutomator2。
    • deviceName:模拟或真实设备的名称。
    • appPackage:应用的包名。
    • appActivity:启动时的主Activity名称。
    • languagelocale:设置语言和区域。
    • udid:设备的唯一标识符,在这里指定了一个本地模拟器的地址。
  3. 初始化WebDriver会话

    • setUp方法中创建了一个WebDriver实例,用于控制设备上的应用。
  4. 清理工作

    • tearDown方法用于在测试结束后关闭WebDriver会话。
  5. 测试方法

    • test_find_by_id 方法包含了一系列操作来验证元素查找的功能。
      • 使用current_packagecurrent_activity来获取当前的包名和Activity名称。
      • 使用find_element通过ID查找元素。
      • 使用find_element通过CLASS_NAME查找元素,并打印相关信息。
      • 使用find_elements通过CLASS_NAME查找多个元素,并遍历输出文本。
      • 使用find_elements通过XPath查找元素,虽然这里查找的是多个元素,但是XPath只匹配了一个元素。
效果
相关推荐
databook8 小时前
Manim实现闪光轨迹特效
后端·python·动效
Juchecar10 小时前
解惑:NumPy 中 ndarray.ndim 到底是什么?
python
用户83562907805110 小时前
Python 删除 Excel 工作表中的空白行列
后端·python
Json_10 小时前
使用python-fastApi框架开发一个学校宿舍管理系统-前后端分离项目
后端·python·fastapi
数据智能老司机16 小时前
精通 Python 设计模式——分布式系统模式
python·设计模式·架构
数据智能老司机18 小时前
精通 Python 设计模式——并发与异步模式
python·设计模式·编程语言
数据智能老司机18 小时前
精通 Python 设计模式——测试模式
python·设计模式·架构
数据智能老司机18 小时前
精通 Python 设计模式——性能模式
python·设计模式·架构
c8i18 小时前
drf初步梳理
python·django
每日AI新事件18 小时前
python的异步函数
python