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

时间:2022-05-04 19:25:00

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')]]")); 

4 个解决方案

#1


19  

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表的子元素。

#2


8  

Answering my own question...This appears to get the exact element.

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

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

#3


1  

I think you can also use the cssSelector

我想你也可以使用cssSelector

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

#4


0  

Try (built this be combining xpath: find a node that has a given attribute whose value contains a string and Getting attribute using XPath)

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

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

#1


19  

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表的子元素。

#2


8  

Answering my own question...This appears to get the exact element.

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

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

#3


1  

I think you can also use the cssSelector

我想你也可以使用cssSelector

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

#4


0  

Try (built this be combining xpath: find a node that has a given attribute whose value contains a string and Getting attribute using XPath)

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

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