Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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')]]"));  
like image 559
Bala Avatar asked Feb 11 '14 12:02

Bala


People also ask

Can you use XPath for HTML?

XML and HTML Note that HTML and XML have a very similar structure, which is why XPath can be used almost interchangeably to navigate both HTML and XML documents.

How do I find the web element using XPath?

Go to the First name tab and right click >> Inspect. On inspecting the web element, it will show an input tag and attributes like class and id. Use the id and these attributes to construct XPath which, in turn, will locate the first name field.

What is XPath attribute?

Definition of XPath attribute. For finding an XPath node in an XML document, use the XPath Attribute expression location path. We can use XPath to generate attribute expressions to locate nodes in an XML document.

What is XPath 1 in HTML?

XPath='1' is not the part of your HTML, it is being added through chropath plugin. You have copied this html with opened chropath tab. If you refresh the page and close the chropath tab, then you will not find xpath=1.


2 Answers

You get the table like this:

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

Select the data-id attribute and get the child element of name table.

like image 58
Moritz Petersen Avatar answered Oct 02 '22 15:10

Moritz Petersen


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

driver.findElement(By.xpath("//*[@data-id='table1']")) 
like image 20
Bala Avatar answered Oct 02 '22 13:10

Bala