如何定位带有 dynamicID 的按钮 [英] How to locate a button with a dynamicID
问题描述
我正在使用 Appium 自动化一个 Android 应用程序,我们需要点击一个带有动态 ID 的按钮.按钮的 ID 为PROFILEBUTTON"或 IDPROFILEMAILBUTTON".除了坐标,还有什么可以用来自动点击这个按钮?
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 之一,如下所示:
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屋!