如何在Appium中定位含指定文本的CONTINUE按钮的XPath
安卓APP中CONTINUE按钮的XPath定位问题
目标按钮的完整元素定位表达式如下:
//android.widget.Button[@text="Flu Shot (Regular) Getting your flu shot is one of the best ways you can protect yourself, your loved ones, and your community from the flu. Book now for up to 6 people. Appointments will take about 5-15 minutes per person. Book an appointment to get your flu shot from an Injection-Trained Pharmacy Professional. Eligibility: Pharmacists can administer flu shots to children aged: 5 and older in Yukon, Alberta, Saskatchewan and PEI; 4 and older in British Columbia; 2 and older in Ontario, Manitoba, New Brunswick, and Newfoundland. Flu shot administration for children under the age of 5 may vary from Pharmacy to Pharmacy—please confirm with your location before booking. CONTINUE"]
需要点击该按钮,但尝试以下XPath表达式均失败:
//*[ends-with(@text,‘(CONTINUE)’)]//*[@class="android.widget.Button" and @index=7]//android.widget.Button[@text="CONTINUE"]
问题分析
- 第一个表达式错误使用了中文引号,且原文本末尾是
CONTINUE而非(CONTINUE),格式和内容都不匹配; - 第二个表达式依赖
index属性,元素位置变化后会失效,稳定性差; - 第三个表达式错误认为按钮的
text属性仅为CONTINUE,实际是包含该词的长文本。
正确的XPath定位方法
以下几种方式均可尝试:
- 使用ends-with匹配文本末尾(注意使用英文引号,确保环境支持XPath 2.0+):
//android.widget.Button[ends-with(@text, 'CONTINUE')] - 使用contains匹配文本包含关系(兼容性更好,适用于多数环境):
//android.widget.Button[contains(@text, 'CONTINUE')] - 使用substring精准匹配末尾(当ends-with不支持时可用):
注://android.widget.Button[substring(@text, string-length(@text)-7)='CONTINUE']string-length(@text)-7是因为CONTINUE长度为8,索引从1开始,所以取末尾8个字符时用总长度减7。
内容的提问来源于stack exchange,提问作者Goms
相关产品推荐
相关产品推荐

