Bala Bala - 3 months ago 14
Ruby Question

How to get HTML5 data attribute using xpath?

How do I get the first table (table1) using xpath for Webdriver?

<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.

This did not work!

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

Answer

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

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