0

I want to tap "Next" button in my view

lazy var Next = app.butons["Next"] Next.tap()

I am getting below error

Failed to get matching snapshot: Multiple matching elements found for <XCUIElementQuery: 0x280b0d4a0>.

1 Answers1

1

Ignoring typos, what XCTest is telling you is that multiple things match what you're trying to identify. It would be helpful to know which one you're targeting specifically, but you could blindly try to tap the first:

app.buttons["Next"].firstMatch.tap()

If you need to get the 2nd you'll have to manipulate the array of matches and grab the element at index 1 (0 is the first, which is cheaper to get using the previous code):

app.buttons["Next"].element(boundBy: 1).tap()

I find getting the 2nd or last matches to be things I do often so I extend XCUIElementQuery with computed properties var secondMatch and var lastMatch, where secondMatch returns the above element and lastMatch is the following:

extension XCUIElementQuery {
  var lastMatch: XCUIElement {
    return self.element(boundBy: self.count - 1)
  }
}
Mike Collins
  • 4,108
  • 1
  • 21
  • 28
  • I get an error `Value of type 'XCUIElement' has no member 'element'` – acmpo6ou Aug 06 '23 at 17:54
  • It is telling you a fact :) You've typed something wrong. You're asking an XCUIElement to return a property of `.element` when it _is_ one. Your object needs to be of type `XCUIElementQuery`. Feel free to open a new question with more detail. – Mike Collins Aug 07 '23 at 17:07
  • I typed exactly what you're showing in your answer: `app.buttons["my button"].element(boundBy: 1).tap()`, and I got the above error. What I'm trying to say is: there is an error in your answer. I was able to find the correct answer elsewhere, don't remember where though. – acmpo6ou Aug 09 '23 at 13:50
  • What is the correct code? – Mike Collins Aug 09 '23 at 18:21
  • Here is the answer that helped me: https://stackoverflow.com/a/39449443/11004423 It shows that you need to use `app.buttons.matchingIdentifier("my_button").element(boundBy: 1).tap()`. That is what worked for me. – acmpo6ou Aug 10 '23 at 11:24
  • Glad it worked for you. That's a very old way to write an XCUIElementQuery. It should function exactly the same. – Mike Collins Aug 10 '23 at 18:31