Getting attribute using XPath and giving inner text using value attribute

xpath contains
xpath get attribute value text()
xpath contains(text)
xpath get text
xpath input(@value)
xpath text
xpath syntax
xpath cheat sheet

Given an XML structure like so:

<table>
    <thead>
        <tr>    
            <th><div data-col="0">First Name</div></th>
            <th><div data-col="1">Last Name</div></th>
            <th><div data-col="2">Age</div></th>
            <th><div data-col="3">Id</div></th>
            <th><div data-col="4">City</div></th>
        </tr>
    </thead>
    <tbody>
        <tr data-row="0">
            <td data-col="0"><div>Ivan</div></td>
            <td data-col="1"><div>Ivanov</div></td>
            <td data-col="2"><div>35</div></td>
            <td data-col="3"><div>EFF-12218</div></td>
            <td data-col="4"><div>London</div></td>
        </tr>
    </tbody>
</table>
  1. How could I get the value of data-col (where text = 'id'), for the first element?
  2. How could I get the value of text in this cell ('EFF-12218') using found value of data-col ?
  1. How could I get the value of data-col (where text = 'id'), for the first element?

String colNumber = driver.findElement(By.xpath("//div[text()='Id']")).getAttribute("data-col");

  1. And how could I get the value of text in this cell ('EFF-12218') using found value of data-col ?

driver.findElement(By.xpath("//td[@data-col='" + colNumber + "']")).getText();

Selecting content on a web page with XPath, Use the XPath syntax to select elements on this web page are contained inside the opening and closing tags; XML attribute nodes contain values that Every HTML element is an element node; The text inside HTML elements are text nodes in the document from the current node - this gives us the ability to “skip levels”. i want to get id text but not getting yet so please help me how to get it Edited by Soniya Singh Wednesday, July 20, 2016 9:06 AM Wednesday, July 20, 2016 9:04 AM

Examples with xpath and Css (ID, Name, Text and Links), As we know it is always better to use ID and Name to locate the elements which will work for sure. We can also define by the using the value attribute //input[@ name='email'][@value='Email or Phone'] Css examples of a link inside of a div tag can be identified as div > a how to get text of multi elemen using xpath? Use cases; Stack Overflow Public questions and answers; Teams Private questions and answers for your team; Enterprise Private self-hosted questions and answers for

To use the value of the attribute data-col as an index to the children of the <tbody> element, use this XPath-1.0 expression

//tbody/tr/td[@data-col=//thead/tr/th[div/text()='Id']/div/@data-col]

Its result is

EFF-12218

as expected. The answer to your first question is, by the way, the following

//thead/tr/th[div/text()='Id']/div/@data-col

which is a part of the second question.

HTML DOM innerText Property, Get the inner text of an element: The innerText property sets or returns the text content of the specified node, and all its descendants. Tip: To set or return the HTML content of an element, use the innerHTML property. Return Value: A String, representing the "rendered" text content of a node and all its descendants � Given the following XML: [code] <books category="science"> <book id="7890"> <title>A New Kind of Science</title> <edition&gt;2&lt;/edition&gt; &lt;author id=&quot

Refer below response it will help you to get text of any cell. you have to just change value of row and column in xpath:

 WebDriver driver = new ChromeDriver();
 // to Get Text of ID( you can store into array/arraylist
// By Changing the row column value in xpath //tbody/tr[row]/td[column]) you can get 
   //data of any cell of table
List<WebElement> id_elements = driver.findElements(By.xpath("//tbody/tr/td[4]"));
int noOfRow= id_elements.size();
for(int i=0; i<=noOfRow;i++) {
    String id = driver.findElement(By.xpath("//tbody/tr["+i+"]/td[4]")).getText();
    System.out.println(id);
    }

Hope it gives you idea!

XML and XPath, XSD Text Only XSD Mixed XSD Indicators XSD <any> XSD <anyAttribute> XPath can be used to navigate through elements and attributes in an XML XPath uses path expressions to select nodes or node-sets in an XML bookstore element that have a price element with a value greater than 35.00 Get Certified �� 2. Java example find xml element with attribute value using xpath. Let’s look at the code which has been used to evaluate above xpath expressions to select nodes having certain attribute value. 2.1. XPath evaluate example. To evaluate xpath in java, you need to follow these steps: Read XML file into org.w3c.dom.Document.

Ultimate XPath Writing Cheat Sheet Tutorial with Syntax and Examples, Recommended Read => Identify Web Elements Using XPath in //*[contains(text (), 'Setting')], This will result in two web elements. getAttribute(“title”)' will go to the specific row and first column and get the value of the title attribute. is placed inside a table and the table is placed inside a form element. Selects all the title elements that have an attribute named lang //title[@lang='en'] Selects all the title elements that have a "lang" attribute with a value of "en" /bookstore/book[price>35.00] Selects all the book elements of the bookstore element that have a price element with a value greater than 35.00 /bookstore/book[price>35.00]/title

Select a node with XPath whose child node contains a specific inner , Getting attribute using XPath, How could I get the value of lang (where I know that I can select an element that has a child with a particular innertext using that XPath statement translates to “Give me the NAME node that has the text value of � Java program to get value from in Java using XPath Below given Java program create a DOM object from supplied XML string. It then applies the XPATH expression using XPath.selectNodes () method. Method returns list of Element instances which are result of evaluating XPath expression.

document . title - HTML Standard, The head attribute, on getting, must return the head element of the document (a Through scripting and using other mechanisms, the values of attributes, text, an element in the DOM can be explicitly referenced is by giving an id attribute to For XML compound documents, these contexts could be inside elements from� The following example explains how to use XPath with XmlDocument and select XML nodes by Attribute values. The XML file is loaded into an XmlDocument and then using XPath all the nodes where City attribute has value London are fetched as XmlNodeList.

Comments
  • Very thorough answer.