Selecting iOS elements by partial text

For the life of me, I can’t figure out how to select a button containing a string. I have a button that says, “Notes (83)” and 83 is the # of new notes. This will change frequently so I want to be able to tap/click on the button when it finds the text “Notes.”

I’m sure I’m just not getting the syntax quite right. Does someone have an example of some Ruby code you use to select by ios predicate string?

I’m using Appium 1.6.3, Xcode 8, XCUITest, and Ruby. This is what I think has me the closest:

wait { find_element(:name => /Notes/).displayed? }

And here’s what I’m seeing in the logs:

Got response with status 200: {"value":{"using":"name","value":"(?-mix:Notes)","description":"unable to find an element"},"sessionId":"5ADFEB84-F8E0-4975-BC67-818A5DD0F1B9","status":7}

Am I not escaping “Notes” correctly? Should I not be using :name? Help? The docs aren’t quite getting me there.

try something like:

// Java
WebElement elem = ((IOSDriver) driver).findElement(MobileBy.xpath("//*[contains(@label, '" + text + "')]"));
WebElement elem = ((IOSDriver) driver).findElement(MobileBy.xpath("//*[contains(@text, '" + text + "')]"));
List<WebElement> el = driver.findElements(MobileBy.xpath("//*[contains(text(), '"+text+"')]"));

but try to use xpath as less as possible - it is slowest option.

Thanks for your help again Aleksei! You’re always the one to answer my questions. :slight_smile:

I tried using path and I didn’t form it correctly, so I just gave up on that and tried other things because I know that XPath isn’t the most stable or performant.

I ended up going with an Rspec assertion, which works:

button("Notes").click if exists { button "Notes" }

But I have a feeling there is a “right” way to do it with XCUITest, so if anyone knows a better way I would love to hear it.

share part of your screen where you see “Notes”. get first page source.

// java
System.out.println(driver.getPageSource()); // will print page source into console

find there node with “Notes” and few more parent nodes also and share with us.

in ruby you have find_exact and find methods for those kinds of situations