# Selecting iOS elements by partial text

**URL:** <https://discuss.appium.io/t/selecting-ios-elements-by-partial-text/15679>\
**Category:** Support\
**Created:** [March 28, 2017, 8:16pm UTC](https://discuss.appium.io/t/selecting-ios-elements-by-partial-text/15679 "2017-03-28T20:16:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Elysia\_Lock](https://discuss.appium.io/user_avatar/discuss.appium.io/elysia_lock/32/3224_2.png) [@Elysia\_Lock](https://discuss.appium.io/u/Elysia_Lock)\
**Post date:** [March 28, 2017, 8:16pm UTC](https://discuss.appium.io/t/selecting-ios-elements-by-partial-text/15679/1 "2017-03-28T20:16:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Aleksei](https://discuss.appium.io/user_avatar/discuss.appium.io/aleksei/32/6267_2.png) [@Aleksei](https://discuss.appium.io/u/Aleksei)\
**Post date:** [March 29, 2017, 9:30am UTC](https://discuss.appium.io/t/selecting-ios-elements-by-partial-text/15679/2 "2017-03-29T09:30:18Z")

</div>

try something like:

```auto
// 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.

---

<div class="post-metadata">

**Author:** ![Elysia\_Lock](https://discuss.appium.io/user_avatar/discuss.appium.io/elysia_lock/32/3224_2.png) [@Elysia\_Lock](https://discuss.appium.io/u/Elysia_Lock)\
**Post date:** [March 29, 2017, 2:54pm UTC](https://discuss.appium.io/t/selecting-ios-elements-by-partial-text/15679/3 "2017-03-29T14:54:08Z")

</div>

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

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.

---

<div class="post-metadata">

**Author:** ![Aleksei](https://discuss.appium.io/user_avatar/discuss.appium.io/aleksei/32/6267_2.png) [@Aleksei](https://discuss.appium.io/u/Aleksei)\
**Post date:** [March 29, 2017, 6:58pm UTC](https://discuss.appium.io/t/selecting-ios-elements-by-partial-text/15679/4 "2017-03-29T18:58:04Z")

</div>

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

```auto
// 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.

---

<div class="post-metadata">

**Author:** ![slipy12](https://discuss.appium.io/letter_avatar_proxy/v4/letter/s/ac8455/32.png) [@slipy12](https://discuss.appium.io/u/slipy12)\
**Post date:** [March 30, 2017, 6:35am UTC](https://discuss.appium.io/t/selecting-ios-elements-by-partial-text/15679/5 "2017-03-30T06:35:35Z")

</div>

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