How to locate a button with a dynamicID

让人想犯罪 __ 提交于 2019-11-29 18:51:39

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')]"));

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')]"));

driver.findElement(By.xpath("//*[contains(@resource-id,'profileMailButton') or contains(@resource-id,'profileMailPremiumButton')]"));

This worked for me.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!