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?
Asked
Active
Viewed 981 times
-1
-
1Could you add its html code? – Anand May 01 '18 at 12:35
-
Try to locate via the different locator - CSS, Name or etc. – Zhivko.Kostadinov May 01 '18 at 13:51
-
There is no unique locator for this element. – sandy May 01 '18 at 14:02
3 Answers
2
You can use partial id
driver.findElement(By.cssSelector("[id*='PROFIL'][id*='BUTTON']"));
Or with xpath
driver.findElement(By.cssSelector("//*[contains(@id, 'PROFIL') and contains(@id, 'BUTTON')]"));
driver.findElement(By.cssSelector("//*[contains(@id, 'PROFIL')][contains(@id, 'BUTTON')]"));

Guy
- 46,488
- 10
- 44
- 88
-
In this case you could (should?) be more specific and use starts with and ends with... `[id^='PROFILE'][id$='BUTTON']` to reduce/eliminate bad matches. – JeffC May 01 '18 at 19:59
-
-
-
-
1
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
$ : To indicate an attribute value ends with
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']"));
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')]"));

undetected Selenium
- 183,867
- 41
- 278
- 352
-
-
@sandy Checkout my answer update and let me know the status – undetected Selenium May 01 '18 at 20:17
-
-
@sandy Can you update me the `tag` name which will contain the element with the mentioned IDs – undetected Selenium May 02 '18 at 08:08
-
resource-id:" net.ilius.android.meetic:id/profileMailButton" or this "net.ilius.android.meetic:id/profileMaiPremiumlButton" I used : driver.findElement(By.xpath("//*[contains(@resource-id,'net.ilius.android.meetic:id/profileMail')]")); – sandy May 02 '18 at 08:17
-
0
driver.findElement(By.xpath("//*[contains(@resource-id,'profileMailButton') or contains(@resource-id,'profileMailPremiumButton')]"));
This worked for me.

sandy
- 117
- 1
- 12