Updated March 28, 2023
Definition of XPath innertext
XPath innertext is used to select the node of innertext using XPath. The expression will specify the pattern which selects the xml nodes, we can also use XSLT template pattern for applying the transformation. The node in expression is referring the more than elements. It is also referring the attribute and text among the other things, XPath innertext is defining the model of an abstract document that defines nodes.
What is XPath innertext?
The XPath expression will be returning the reference of html element whose text contains the data entry. It will be returning the element, not returning the text. The property of inner text will be returning the content of an element. The abstract document model is defining the document model of the abstract which will define the seven kinds of nodes as follows.
- Root
- Element
- Text
- Comment
- Attribute
- Namespace
- Processing instruction
Innertext provides the rule for converting node in a document object module of XML to a boolean, string value of double. As we know that XPath is a w3c defined language which is defined in the recommendation of the w3c xml path. The file of xml is loaded into the document of xml and then we can use XPath nodes. It is not able to find the complicated web element for the test script, we need to use the functions of the XPath 1.0 library. By using the function combination, we can create the XPath which was more specific.
We can use functions with XPath innertext used to search the web element which contains the particular text. We can extract all the elements witch matches the text value using the innertext function throughout the web page. We can get the text from the web element with the webdriver of selenium. We can use the gettext method will obtaining the innertext from an element. It will be fetching the text from an element that was visible along with the sub-elements. Using innertext we can ignore the leading and trailing spaces. We need to identify the element with the help of the class, id, name, CSS, and XPath which then applies the get text element.
XPath innertext find text
Below are the steps shown to find the text by using XPath, innertext is as follows. In the below example, we are creating the project of Innertext. In the below example, we are creating the template of project name as XPath innertext into the spring boot.
- While creating the project into the XPath innertext by using spring boot we provided the name of the group as com.example, artifact name as XPathInnertext, project name as XPathInnertext, and selected java version as 11.
Group – com.example
Artifact name – XPathInnertext
Name – XPathInnertext
Spring boot – 2.6.7
Project – Maven
Java – 11
Package name – com.example. XPathInnertext
Project Description – Project for XPathInnertext
Dependencies – Spring web
Packaging – Jar
2. In the below figure we can see that extracting the project name as XPathInnertext and opening this project by using the spring tool suite.
3. In the below step we are checking the structure of the innertext project. Also, we are seeing the project dependency which was added to the pom.xml file. Also, we are checking all the structures of the innertext example project as follows.
4. In below we are adding the XPath dependency into the XPathInnertext We need to add the dependency in as tag of dependency.
Code –
<dependency>
<groupId> net.sf.saxon </groupId>
<artifactId> saxon-XPath </artifactId>
<version> 8.7 </version>
</dependency>
5. In the below example we are creating the XPath_innertext.html file to find the text from the XPath innertext as follows.
Code –
<!DOCTYPE html>
<html>
<body>
<h1> XPath innertext find text </h1>
<h2> XPath innertext </h2>
<button id = "XPath"> Try now </button>
<p> XPath innertext find text by using XPath </p>
<p id = "XPath innertext"> </p>
<p> XPath innertext is used to select the node of innertext using XPath. </p>
<script>
let text = document.getElementById("XPath").innerText;
document.getElementById ("XPath innertext").innerHTML = text;
</script>
</body>
</html>
XPath innertext selection
XPath expression will have composed of the location path and the optional predicates. Expression is including the variables of XPath. In the below example, we are creating the xml file of student records as follows. We have already created the Innertext project so now we have no need to create it again.
Code –
<?xml version = "1.0" encoding = "UTF-8"?>
<Students>
<Student>
<StudID> 1 </StudID>
<Name>
<FName> ABC </FName>
<Sname> PQR </Sname>
</Name>
<Sex> Male </Sex>
<DOB> 01/02/2017 </DOB>
<Email> NA </Email>
</Student>
<Student>
<StudID> 2 </StudID>
<Name>
<FName> PQR </FName>
<Sname> XYZ </Sname>
</Name>
<Sex> Female </Sex>
<DOB> 05/03/2017 </DOB>
<Email> NA </Email>
</Student>
<Student>
<StudID> 3 </StudID>
<Name>
<FName> CBD </FName>
<Sname> PQR </Sname>
</Name>
<Sex> Male </Sex>
<DOB> 09/07/2017 </DOB>
<Email> NA </Email>
</Student>
</Students>
Now we are creating the java file of innertext selection as follows. We are creating the class name as XPath_innertext.
Code –
public class XPath_innertext {
…………..
}
Examples
Below is the example of innertext as follows. In the below example first, we are creating the emp.xml file as follows.
Code –
<?xml version = "1.0" encoding = "UTF-8"?>
<Employees>
<Employee>
<EmpID> 1 </EmpID>
<Name>
<Emp_FName> ABC </Emp_FName>
<Emp_SName> PQR </Emp_SName>
</Name>
<Sex> Male </Sex>
<Addr> Pune </Addr>
</Employee>
<Employee>
<EmpID> 2 </EmpID>
<Name>
<Emp_FName> PQR </Emp_FName>
<Emp_SName> XYZ </Emp_SName>
</Name>
<Sex> Female </Sex>
<Addr> Mumbai </Addr>
<EmpID> 3 </EmpID>
<Name>
<Emp_FName> ABC </Emp_FName>
<Emp_SName> XYZ </Emp_SName>
</Name>
<Sex> Female </Sex>
<Addr> Mumbai </Addr>
</Employee>
</Employees>
Now we are creating the java file of are as follows. We are creating the class name as XPath_innertext.
Code –
public class XPath_innertext {
…………..
}
Conclusion
The XPath expression will return the reference of html element whose text contains the data entry. Innertext is used to select the node of innertext using XPath. The expression will specify the pattern which selects the xml nodes. XPath innertext is very useful and important.
Recommended Articles
This is a guide to XPath innertext. Here we discuss the Definition, What is XPath index, How to use XPath index and examples with code implementation respectively. You may also have a look at the following articles to learn more –