Home > Blockchain >  How to select same xpath level to click in Selenium
How to select same xpath level to click in Selenium

Time:11-07

I want to click on a button that is in the SAME xpath, all the values ​​of the different objects are the same, except for the name. For example, if the name is "NAME ONE", I just want to click on the button that belongs to the name at the same level. I have tried xpath, and it can find the name and print it in console, but I don't know how to click on the element that is at the same level without losing the context of the name in which I am.(Items may be out of order). I'm using Python3

<div> something[A]
  <div> something [B]
    <span> NAME ONE </span>
  </div>
  <div> something [C]
    <span> something [D] </span>
  </div>
  <div> something [E]
    <button> I WANT CLICK THIS BUTTON </button>
  </div>
</div>

<div> something[A]
  <div> something [B]
    <span> NAME TWO </span>
  </div>
  <div> something [C]
    <span> something [D] </span>
  </div>
  <div> something [E]
    <button> I DO NOT WANT CLICK THIS BUTTON </button>
  </div>
</div>

<div> something[A]
  <div> something [B]
    <span> NAME THREE </span>
  </div>
  <div> something [C]
    <span> something [D] </span>
  </div>
  <div> something [E]
    <button> I DO NOT WANT CLICK THIS BUTTON </button>
  </div>
</div>

CodePudding user response:

Maybe:

//*[contains(div, 'NAME ONE')]/div/button

Simplest way I could get xpath to work like that.

CodePudding user response:

this xpath does the job:

//span[contains(text(),'NAME ONE')]/ancestor::div[contains(text(),'something[A]')]//button
  • Related