How to find an element by name/text/value that includes something?

So of course before posting this I’ve searched on this forum as well as read through Appium-Ruby library, but I couldn’t figure something I know I’ve done in the past and I can’t remember how I did it:

how to find an element that includes some string or an integer in it’s name/text/value …?
For example find_element(:name, includes"Something here that is part of the entire name").click
I’ve found this “(Textfield) textfield_include(text) Get the first textfield that includes text.” but can’t figure out how to actually write it.

The reason why I need this is because too many times xpaths change if something happens within the application and I do not want to rely on them. For example instead of pointing to xpath xxxxxxxx/cell1 I’d rather just look for an element that has something in it’s name. Hope it manes sense.

As always, thank you!

You can do it using UiSelector parameter inside FindElementByAndroidUIAutomator

Ex : findElementByAndroidUIAutomator(“new UiSelector().textContains(”"+partialtext+"")")

More details on UISelector @ http://developer.android.com/reference/android/support/test/uiautomator/UiSelector.html#descriptionContains(java.lang.String)

@RamS i forgot to mention that I am doing this for iOS platform.

You can use regular expressions. For example, if you are looking for a button “Low Resolution” with a label, name/txt, ID etc…
You can do something like this:

@driver.find_element(:name => /^Low/)

This will find the first element with “Low” at the beginning of its name

For reference:
http://ruby-doc.org/core-2.1.1/Regexp.html

Hello @Chris_Billante,

I tried using your suggestion, but I just can’t get it to work.
I am trying the following (element_name value is being passed in):

wait { find_element(:name => /’^’ + element_name/).displayed?}

also tried the following:

wait { find_element(:name, Regexp.new(’^’ + element_name)).displayed?}

Any ideas what else I could try?

Hello @Liudas

I`m using something like this in C# and it works fine for me, i think if you cannot make findbyName work, you should also try Xpath in iOS it might be a bit slower but it will find the first element with the specific text : mDriver.FindElementByXPath("//*[@name=’" + text + “’]”);

You can change @name with value or id if it is the case, or even create a method that searchers via all 3 until it finds a results with at least one of them.

or you could try : XPath = “//*[contains(@value=‘page 1 of’)]” this will not search an exact match but a text that contains the given string.

You can also use this if you want multiple elements found by adding them to a List<>

Regards,
-Gery-

1 Like

Try this way.

driver.findElement(By.xpath("//android.widget.TextView[@text=‘ADD’]"))

1 Like

Hey @Liudas

Use this
//[contains(text(),’’)]
//
[contains(@id,’’)]
//*[contains(@xpath,’’)]
etc

1 Like

Hey Liudas,

You can build xpath like contains text like below

driver.findElement(By.xpath("//contains[text(),“xyz”)

Hope this helps

1 Like

I would like to emphasize that if you are using Xpath in Appium, text() function will not work.
At least for Android (checked just now).

But this way it will work:
driver.findElement (By.xpath ("//*[contains(@text, ‘apple’)]"));

The thing is that you search for the parameter named ‘text’, so you add @ at the beginning.
It’s just like when you search element by ID in Xpath using @resource-id parameter.

You can also add some logical operators (OR / AND):
driver.findElement (By.xpath ("//*[contains(@text, ‘apple’) or contains(@text, ‘orange’)]"))

1 Like