Search code examples
seleniumdomxpathrelative-path

How can I find my web element using the correct relative xpath


I am having trouble identifying a specific web element from the DOM. It is a button element, and there are 4 of them in the DOM, exact the same. The one that I need is the third one from the top.

So far I have written the following Xpath:

//button[@class='btn btn-success add-instrument']

This gives me 4 options. I have also tried:

//button[contains(text(), 'texthere')]

This also gives me 4 options. I have tried traversing from above and below, but that does not work either.

I want to: Index the third element, but can't get it to work.

What I have tried (with both options, mentioned above)

//button[contains(text(), 'texthere')]//[3]

I have also tried other indexes, but it finds nothing.

SOLVED BY USING: "(//button[@class='btn btn-success add-instrument'])[3]"


Solution

  • Try this below xpath.It will identify the 3rd button.

    "(//button[contains(text(), 'texthere')])[3]"
    

    or

    "(//button[@class='btn btn-success add-instrument'])[3]"