在Appium和Python中查找元素的子元素,可以使用XPath来定位。下面是一个使用Appium和Python查找子元素的代码示例:
from appium import webdriver
# 设置Desired Capabilities
desired_caps = {
"platformName": "Android",
"deviceName": "Android Emulator",
"appPackage": "com.example.app",
"appActivity": "com.example.app.MainActivity"
}
# 连接Appium服务器
driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps)
# 定位父元素
parent_element = driver.find_element_by_xpath("//android.widget.LinearLayout[@resource-id='parent_element_id']")
# 查找子元素
child_elements = parent_element.find_elements_by_xpath("//android.widget.TextView")
# 遍历子元素并输出文本
for child_element in child_elements:
print(child_element.text)
# 关闭Appium会话
driver.quit()
在上面的代码中,首先设置了Desired Capabilities,然后连接到Appium服务器。然后,使用XPath定位父元素,然后使用find_elements_by_xpath
方法查找子元素。最后,使用一个循环遍历子元素,并输出文本。最后,关闭Appium会话。
请确保将"parent_element_id"
替换为您要查找的父元素的资源ID,并将"com.example.app"
和"com.example.app.MainActivity"
替换为您的应用程序的包名和主活动。