How to get an element by name, if part of name is dynamic

How to get an element by name, if part of name is dynamic and changes every time test is run.

e.g, below read is static while remaining changes.

read today - 9:20

read yesterday - 10:30

You don’t say what language you are using, or what platform, or what element type, so I’ll just go with Ruby/iOS/Button:

    buttons = driver.tags('UIAButton')
    buttons.each do |button|
      # 'read' text varies, so use an include
      if button.text.include?('read')
        return button

I am using iOS with Java client, is it possible you can give me a java code snippet.


I’m a bit rusty on Java, so you may need to adjust. That said, it’s pretty similar. Something like:

List<IOSElement> buttons = driver.findElementsByClassName("UIAButton")
for (IOSElement button :  buttons) {
  if (button.text.contains('read') {
    return button;

Of course you’ll want to wrap that up in a method to use the return properly. If somebody see a problem with my Java please correct :wink:

1 Like

Thanks that helps :slightly_smiling:
below is the java code i used

List<MobileElement> sTexts=	driver.findElementsByClassName("UIAStaticText");

for (MobileElement melement:sTexts)
		System.out.println("found Read");
1 Like