Below is the XPath-


The numeric value in the above XPath changes and is not static. There are more then 10 buttons on the screen, I want to click on the first one.

Text1                 btn1   //let’s say btn1 contains 8/13

Text2                 btn2   //let’s say btn1 contains 4/8

Can anyone help me to achieve this task?

  • What other attributes can you access? Do any of these not change? You would need to change your XPath to use the different search criteria, but other than that it should be fairly straightforward.
    – Kate Paulk
    Oct 22 '18 at 19:39
  • @kate I do have id other than this XPATH which is also dynamically changing with number like 8/3, 4/8. And I need to select the first occurrence. Oct 23 '18 at 10:46
  • If you need the first occurrence of a button, //XCUIElementTypeButton[1] should do it. (Disclaimer: I am not an Xpath expert)
    – Kate Paulk
    Oct 23 '18 at 11:22

I think in XPath you are using the name or value may be keep on changing, so can go for the concept of following sibling for getting the proper XPath.



If you are specifically mentioning as you want to click on the first button no matter the text, then I would go List concept

@FindBy(xpath = "//XCUIElementTypeButton")
    private List<MobileElement> button;


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.