'Finding screen elements in appium(iOS) using contains

I am doing QA and recently started using appium and cucumber to automate some tests and am still new to this.

I succeeded in what I wanted to automate using some functions like this one.

def function(element_name)
    find_element(:xpath,
                 '//XCUIElementTypeOther[@name="' + element_name + '"]'
                ).click
end

This works for what I want, but now I am trying to redo the same functions but using contains. Something like this

def function(element_name)
  find_element(:xpath,
               '//*[contains(text(), element_name)]'
              ).click
end

What I'm getting is

An element could not be located on the page using the given search parameters.

I think I am just not using contains the right way but I am really not sure.



Solution 1:[1]

Xpath is not a good way for searching elements in Appium/XCUITest as it is often too slow and might not work as you expect it to work (your case with contains).

Instead you should you try XCUITest native locator strategies: iOSNsPredicate or iOSClassChain, e.g.

driver.find_element_by_ios_predicate("label contains 'Some text'")

You can check more examples here: python client tests, java client tests

Solution 2:[2]

the way you are using contains is correct, the only problem is its not finding the element. If XPATH is not working , why dont you try with relative path e.g. below

def function(element_name)
  //tbody//td[contains(text(),element_name)]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 pankaj mishra