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.
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
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<>
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’)]"))
Hi, @Gery, Im trying to store element have same partial id but getting “0” in result.
I want to get two element in list and print them.
Resource ID for 1st Element is: com.company:id/filterDataTournament
Resource ID for 2nd Element is: com.company:id/filterDataTournament_2
List options = driver.findElements(By.xpath("//*[starts-with(@resource-id,‘com.company:id/filterDataTournamen_’)]")) ;