如何定位具有属性 xpath="1" 和 xpath="2" 的子节点

How to locate child nodes having attribute xpath="1" and xpath="2"

我试图找到一个具有两个子节点的网络元素:

<div _ngcontent-c2="" class=" " title="Twelve (Start date is 31| 0 user)" xpath="1"></div>
<div _ngcontent-c2="" class=" " title="Twelve (Start date is 31| 0 user)" xpath="2"></div>

我尝试 //div[contains(@title,'Twelve (Start date is 31| 0 user)')][1] 获取第一个元素,但对我不起作用。

你可以试试 CSS:

div[xpath='1']

div[xpath='2']

xpath="1"

xpath="1"属性是xpath v1.0

的引用

xpath="2"

xpath="2"属性是xpath v2.0

的引用

备案 Selenium 仅支持 XPath v1.0

多一点 outerHTML 包括 parent 标签 会帮助我们构建一个更规范的答案。此外,关于您正在使用的 Language Binding 的信息也丢失了。但是,要找到第一个元素,您需要为所需的 WebDriverWait 引入 visibilityOfElementLocated() 并且您可以使用以下 xpath:

  • Java:

    WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(@title,'Start date is 31') and @xpath='1']")));
    
  • Python:

    element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[contains(@title,'Start date is 31') and @xpath='1']")))
    

Here you can find a detailed discussion on