如何 select 相同的 xpath 级别在 Selenium 中单击

How to select same xpath level to click in Selenium

我想点击一个在同一个xpath中的按钮,不同对象的所有值都是相同的,除了名称。比如名字是“NAME ONE”,我只想点击同级属于这个名字的按钮。我已经尝试过 xpath,它可以找到名称并在控制台中打印它,但我不知道如何在不丢失我所在名称的上下文的情况下单击同一级别的元素。(项目可能不正常)。我正在使用 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>

也许:

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

让 xpath 像那样工作的最简单方法。

这个 xpath 可以完成工作:

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