如何使用 appium 以通用方式查找使用文本的元素,以实现 ios 自动化?



我是使用 appium 的 ios 自动化新手。我正在尝试在 python 中获取基于应用程序自动化中的文本的元素。我目前正在使用以下命令获取文本:

uiel = self.driver.find_element_by_xpath('//XCUIElementTypeStaticText[@label="abc"]')

假设我想为 IOS 编写一个 api find_element_by_text(字符串),我必须传递元素和字符串的类型,并根据我应该调用的元素类型,按x_path find_element,如下所示:

if ios_button:
uiel = self.driver.find_element_by_xpath('//XCUIElementTypeButton[@label="'+ aString +'"]')
elif is_other:
uiel = self.driver.find_element_by_xpath('//XCUIElementTypeOther[@label="' + aString + '"]')
elif is_cell:
uiel = self.driver.find_element_by_xpath('//XCUIElementTypeCell[@label="' + aString + '"]')
else:
uiel = self.driver.find_element_by_xpath('//XCUIElementTypeStaticText[@label="' + aString + '"]' )

有没有办法只用一行而不是上面所有的检查来获取IOS中的元素?

将元素类型作为与类型关联的实际字符串("按钮"、"其他"、"单元格"、"静态文本")发送,然后使用串联创建查找,您已经证明您已经理解了。

uiel = self.driver.find_element_by_xpath('//XCUIElementType' + myElementType + '[@label="'+ aString +'"]')

我没有测试运行这段代码;可能存在语法错误,但你应该明白这一点。

最新更新