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')]]"));
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.
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.
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.
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.
You get the table like this:
//span[@data-id='table1']/table
Select the data-id
attribute and get the child element of name table
.
Answering my own question...This appears to get the exact element.
driver.findElement(By.xpath("//*[@data-id='table1']"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With