Definition of XPath First Element
XPath first element is used to locate the first element from the specified string. It is the predefined method of the language of XPath that is used in a statement of XPath for locating the node of the first element from the node type. Basically, the first element method in XPath will select the first element out of the all element which was present in a string for the first element of XPath.
What is the XPath first element?
- First element is returning the position of the first element from the current context. Sometimes we need to find which element is coming to first and which was next, for finding the first element from the set we are using XPath first element.
- First element is very important for defining the template for the first occurrence for the given element or it is also useful for testing a given node from the first in a set of nodes to which it belongs.
- The first element will be returning a number from size as per the context evaluation of the expression.
- We can find the first element by using the following different ways are as follows.
- Find the first element by name.
- Find the first element by id.
- Find the first element by class name.
- Find the first element by XPath.
- Find the first element by link text.
- Find the first element by CSS selector.
- Find the first element by tag name.
- Find the first element by partial link text.
- In order to get the first element of an ID or name of an element, we can use the XPath to display the first value of an element.
How to XPath the first element?
- The below steps show how we can XPath the first element are as follows. We are creating the project name as XPathFirstElement. In the below step we are creating the project template of first element in spring boot.
- At the time of creating the project we are providing project group name as com. example, artifact name as XPathFirstElement, project name as XPathFirstElement, and selected java version as 11. We are defining the version of spring boot as 2.6.7.
Group – com.example
Artifact name – XPathFirstElement
Name – XPathFirstElement
Spring boot – 2.6.7
Project – Maven
Java – 11
Package name – com.example. XPathFirstElement
Project Description – Project for XPathFirstElement
Dependencies – Spring web
Packaging – Jar
2. In this step we are extracting the downloaded project and opening the same by using the spring tool suite as follows.
3. In this step we are checking all the project structures and their files are as follows. Also, we are checking the pom.xml file is created or not. Suppose this file is not created then we need to create the same manually. In the below example this file is created, so we have no need to create it manually.
4. In this step we are adding a dependency in the first element project. We are adding dependencies as follows.
Code –
<dependency>
<groupId> net.sf.saxon </groupId>
<artifactId> saxon-XPath </artifactId>
<version> 8.7 </version>
</dependency>
5. Below is the view of first element from the element array finder. It will not retrieving the underlying element.
Code –
<ul class = "letter">
<li> A </li>
<li> B </li>
<li> C </li>
<li> D </li>
<li> E </li>
<li> F </li>
<li> G </li>
<li> H </li>
<li> I </li>
<li> J </li>
<li> K </li>
<li> L </li>
<li> M </li>
<li> N </li>
<li> O </li>
<li> P </li>
<li> Q </li>
<li> S </li>
<li> T </li>
<li> U </li>
<li> V </li>
<li> W </li>
<li> X </li>
<li> Y </li>
<li> Z </li>
</ul>
6. Below is the code of XPath first element from the element array finder.
Code –
public class FirstElement {
let elefirst = element.all (by.css('.items li')).first();
expect (elefirst.getText ()).toBe ('First')
}
Enable XPath selection
- Google chrome provides the debugging tool which was built called chrome dev tools. It will include many handy features which were used to evaluate the CSS selector’s without any extensions of third-party tools.
- We can also execute the token from the console panel which was used to validate and evaluate the same.
- In the below example we are enabling the XPath selection as follows. We are enabling the selection in the following example.
- XPath uses the expression of the path for selecting a node in the xml document. Nodes are selected by using the following path. Below is the most useful expression of XPath selection as follows.
- Nodename – It will select all the nodes with the name as node name.
- / – This is used to select from the root node.
- // – This symbol is used to select the nodes from the current node which will match the selection
- . – This is used to select the current node.
- @- It is used to select the attribute.
- We can use the search function inside the elements panel for evaluating the CSS selector’s XPath and also we can highlight the matching rows from the DOM.
XPath First Element Array
- XPath first element array can be nested, we can say that members of an array can be attribute values including the sequences, function, maps and other values. The array member contains a sequence that is empty.
- The first element of an array contains a single item, so it contains the sequence of an array.
- As per sequence, the members of an array are addressed by using an integer in the range of 1 to N.
- XPath’s first element array is XDM values which are immutable. At the time of appending, removing an entry from an array we will get a new array.
- By using the sequence and maps first element array doesn’t have an intrinsic type in an element array.
- Below is the example.
Code –
(/Students/stud[@name='PQR'])[1]
Conclusion
The XPath first element is returning the position of first element from the current context. XPath first element is the predefined method of language of XPath is used in a statement of XPath for locating the node of first element from the node type.
Recommended Articles
This is a guide to XPath First Element. Here we discuss the Definition, What is the XPath first element, How to XPath the first element , examples with code implementation respectively. You may also have a look at the following articles to learn more –