Appium自动化(4) - 定位

1|0# 可定位的控件属性

App中的属性 等价 Web 中的属性 代码表达式 注意点
resource-id id driver.find_element_by_id("id")
driver.findElement(By.id("id"))
在App中,id可能不是唯一的
class class/tag driver.find_element_by_class_name("class")
driver.findElement(By.className("class"))
xpath:
//class[id = "id"]
1、当使用 class_name 去定位时,可以理解成类名
2、当使用xpath定位时,class就是tag(标签名),并不是类名。
text name driver.find_element_by_name("name")
driver.findElement(By.name("name"))
name是一个属性 (appium)开始废弃了该方法
content-desc driver.find_element_by_accessibility_id("desc") content-desc属性是用来描述该元素的作用的

2|0定位入门

软件:微博国际版

import time from appium import webdriver desired_caps = { "platformName": "android", "deviceName": "bc3ef5d5", "appPackage": "com.weico.international", "appActivity": ".activity.MainFragmentActivity", "noReset": True, "newCommandTimeout": 6000 } driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps) test = driver.find_element_by_class_name("android.widget.TextView") print(test) print("打印1:" + test.text) test = driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/androidx.drawerlayout.widget.DrawerLayout/android.view.ViewGroup/android.widget.RelativeLayout/android.widget.TabHost/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.FrameLayout/androidx.recyclerview.widget.RecyclerView/android.widget.LinearLayout[2]/android.widget.RelativeLayout[1]/android.widget.TextView[1]") print("打印2:" + test.text) test = driver.find_element_by_xpath("//*[contains(@text,'冈瓦纳')]") print("打印3:" + test.text) >>> 打印1:查看新微博 打印2:冈瓦纳 打印3:冈瓦纳

为什么打印1是,查看新微博?

因为多个class_name的名字是一样的,find_element_by只返回第一个。如果要全部返回,则改成find_elements_by匹配多个元素,再更具索引去匹配

driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps) test = driver.find_elements_by_class_name("android.widget.TextView") for i in test: print("打印1:" + i.text) >>> 打印1:查看新微博 打印1:全部 打印1: 打印1:烫师傅 打印1:11 分钟前 打印1:iQOO 3 5G性能旗舰 ...

__EOF__

本文作者😎
本文链接https://www.cnblogs.com/dongye95/p/15025691.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   dongye95  阅读(61)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示