简体   繁体   English

如何选择相同的 xpath 级别以在 Selenium 中单击

[英]How to select same xpath level to click in Selenium

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

Maybe:也许:

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

Simplest way I could get xpath to work like that.我可以让 xpath 像那样工作的最简单方法。

这个 xpath 可以完成这项工作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM