Anyone know how to find an element by PARTIAL id?

Good day,

i.e. if I have ids in my app like these

id1 = apple_123
id2 = apple_456

And I want all elements that have id beginning with ‘apple’.

is there any way to get these elements at once? Perhaps something like this,

element = driver.findElements(By.Id("apple_*");

Please let me know if this is possible and if not what alternatives worked for you?

Best Regards,
Alan

I use xpath for this sort of thing:
By.xpath(//(element_tag)[starts-with(@id,‘apple_’)])

where “(element_tag)” is the tag of the element from the element tree.

2 Likes

You can use below method
starts-with()
ends-with()
contains()

element = driver.findElements(By.xpath("//[starts-with(@id, ‘apple_’)]");
element = driver.findElements(By.xpath("//
[contains(@id, ‘apple_’)]");
element = driver.findElements(By.xpath("//*[ends-with(@id, ‘apple_’)]");

2 Likes

Thank you so much for helping me out. Will try this out and see. :slight_smile:

did this workd for u?

As @amitjaincoer191 suggested use Xpaths , or u can use UIAutomator locators.