如何找到带有dynamicID的按钮 [英] How to locate a button with a dynamicID
问题描述
我正在使用Appium自动化一个Android应用程序,我们需要单击带有动态ID的按钮.该按钮具有ID"PROFILEBUTTON"或ID"PROFILEMAILBUTTON".除了坐标之外,还有什么可以用来自动单击此按钮的?
I am automating an Android app using Appium where we need to click a button with a dynamic ID. Either the button has ID "PROFILEBUTTON" or ID "PROFILEMAILBUTTON". Apart from co-ordinates, what else can be used to automate clicking this button?
推荐答案
要标识具有动态ID 的元素,您可以 PROFILEBUTTON 或 PROFILEMAILBUTTON 可以将cssSelector与以下通配符一起使用:
To identify an element with dynamic ID either PROFILEBUTTON or PROFILEMAILBUTTON you can use cssSelector with the following wildcards :
-
^:表示属性值以
^ : To indicate an attribute value starts with
$:表示属性值以
因此,最精细的定位器将包括寻找初始字母(即 PROFILE )和结束字母(即 BUTTON )的策略,并且应为:
So the most granular locator would include the strategy to lookout for the initial letters i.e. PROFILE and the ending letters i.e. BUTTON and should be :
driver.findElement(By.cssSelector("[id^='PROFILE'][id$='BUTTON']"));
更新
根据您的评论更新,您可以使用以下等效的 xpath 之一:
Update
As per your comment update, you can use either of the equivalent xpath as follows :
driver.findElement(By.xpath("//*[contains(@resource-id,'profileMail') and contains(@resource-id,'Button')]"));
//or
driver.findElement(By.xpath("//*[contains(@resource-id,'profileMailButton') or contains(@resource-id,'profileMailPremiumButton')]"));
这篇关于如何找到带有dynamicID的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!