简体   繁体   English

如何使用xpath获取HTML5数据属性?

[英]How to get HTML5 data attribute using xpath?

How do I get the first table (table1) using xpath for Webdriver? 如何使用xpath为Webdriver获取第一个表(table1)?

<span id="dynamically generated id" data-id="table1">
  <table>
  ...
  </table>
</span>

<span id="dynamically generated id" data-id="table2">
  <table>
  ...
  </table>
</span>

I am able to get all data-id elements but I want to filter within it for text table1 to get the exact element. 我能够获取所有data-id元素,但我想在其中过滤文本table1以获取确切的元素。

This did not work! 这没用!

driver.findElement(By.xpath("//@*[starts-with(name(),'data-id') [contains(text(),'table1')]]")); 

You get the table like this: 你得到这样的表:

//span[@data-id='table1']/table

Select the data-id attribute and get the child element of name table . 选择data-id属性并获取name table的子元素。

回答我自己的问题......这似乎得到了确切的要素。

driver.findElement(By.xpath("//*[@data-id='table1']"))

我想你也可以使用cssSelector

driver.findElement(By.cssSelector("[data-id='table1']"));

尝试(构建此组合xpath:查找具有给定属性的节点,其值包含字符串使用XPath获取属性

driver.findElement(By.xpath("//*[contains(@data-id, 'table1')]/@data-id"));

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

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