Search code examples
pythoniosautomated-testsappium

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

  • 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