Updated March 28, 2023
Definition of XPath Multiple Attributes
XPath multiple attributes are written by using the web element, as per the situation we can use multiple attributes, or also we can use the single attribute for finding the web page element. While using the single attribute on XPath expression it will return the matching element then we can use the single attribute only. XPath multiple attributes are very important and useful in XPath to retrieve multiple elements.
Overviews XPath Multiple Attributes
In some cases, using the single attribute in the expression of XPath will return the multiple matching elements on the web page. After returning the multiple-element on the web page we can use the multiple attributes in the expression of XPath till we get the matching element. To determine the element we can take the help of an attribute within the tag of html or we can also use the attribute combination and the values for identifying the element. Suppose the id attribute is not present then we can use the other attribute like class and name.
How XPath multiple attributes work?
- At the time of working XPath multiple attributes, we can use two or more attributes in a single class. We can utilize the distinct attribute available for only the tag or attribute combination and values for identifying the element, for the same we need to use the XPath multiple expression.
- If the XPath expression in a single attribute identifies the multiple elements, then we can use more than one attribute in the expression of the path for locating a single element.
- For writing multiple attributes, we can apply the AND and OR conditions. Below is the syntax of multiple attributes by using AND conditions are as follows.
Syntax –
Name_of_tag [@name_of_attribute1 = 'val1' AND @name_of_attribute2 = 'val2']
- Below is the syntax of multiple attributes by using OR conditions are as follows.
Syntax –
Name_of_tag [@name_of_attribute1 = 'val1' OR @name_of_attribute2 = 'val2']
- In the above syntax, the tag name specifies the name of the tag which we have defined in multiple attributes.
- Name of the attribute is defining the attribute name which we are using in multiple attributes, we can use multiple attributes in a single line code.
- Value is defining the value which we are using in the XPath attribute, we can use multiple values as per attribute in a single line code.
- We are using OR and AND conditions at the time of defining the XPath multiple attributes.
- The below steps show how we can work with multiple attributes as follows. We are creating the project name as XPathMultipleAttributes. In the below step we are creating the project template of multiple attributes in spring boot.
1. At the time of creating the project we are providing project group name as com.example, artifact name as XPathMultipleAttributes, project name as XPathMultipleAttributes, and selected java version as 11. We are defining the version of spring boot as 2.6.7.
Group – com.example
Artifact name – XPathMultipleAttributes
Name – XPathMultipleAttributes
Spring boot – 2.6.7
Project – Maven
Java – 11
Package name – com.example. XPathMultipleAttributes
Project Description – Project for XPathMultipleAttributes
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 multiple attributes We are adding dependency as follows.
Code –
<dependency>
<groupId> net.sf.saxon </groupId>
<artifactId> saxon-XPath </artifactId>
<version> 8.7 </version>
</dependency>
5. Now we are creating stud.xml by defining the multiple attribute by using AND condition are as follows. In below example also we are running the project.
Code –
<html>
</head>
<body>
<table align = "center" width = 80% cellspacing = "2" cellpadding = "2" >
<tr>
<td> Stud1 : </td>
<td> input[@type = "text" and @stud_name="ABC"] </td>
</tr>
<tr>
<td> Stud2 : </td>
<td> input[@type = "checkbox" and @name="PQR" class = "4th"] </td>
</tr>
<tr>
<td> Stud3 : </td>
<td> input[@type="text" and name="XYZ"] </td>
</tr>
<tr>
<td> Stud4 : </td>
<td> input [@type = "checkbox" and name = "CBD"] </td>
</tr>
<tr>
<td> Stud5 : </td>
<td> input[@type="password" and class = "password"] </td>
</tr>
</table>
</body>
</html>
XPath multiple attributes Examples
- Below is the example of multiple attributes as follows. In the below example we are using the OR condition as follows.
Code –
<html>
</head>
<body>
<table align = "center" width = 90% cellspacing = "2" cellpadding = "2" >
<tr>
<td> Emp1 : </td>
<td> input[@type = "text" OR @Emp_name="ABC"] </td>
</tr>
<tr>
<td> Emp2 : </td>
<td> input[@type = "checkbox" OR @Emp_name="PQR" class = "4th"] </td>
</tr>
<tr>
<td> Emp3 : </td>
<td> input[@type="text" OR Emp_name="XYZ"] </td>
</tr>
<tr>
<td> Emp4 : </td>
<td> input [@type = "checkbox" OR Emp_name = "CBD"] </td>
</tr>
<tr>
<td> Emp5 : </td>
<td> input[@type="password" OR class = "password"] </td>
</tr>
</table>
</body>
</html>
- Below is the example of XPath multiple attributes as follows. In the below example we are using the AND condition as follows.
Code –
<html>
</head>
<body>
<table align = "center" width = 90% cellspacing = "2" cellpadding = "2" >
<tr>
<td> Emp1 : </td>
<td> input[@type = "text" AND @Emp_name="ABC"] </td>
</tr>
<tr>
<td> Emp2 : </td>
<td> input[@type = "checkbox" AND @Emp_name="PQR" class = "4th"] </td>
</tr>
<tr>
<td> Emp3 : </td>
<td> input[@type="text" AND Emp_name="XYZ"] </td>
</tr>
<tr>
<td> Emp4 : </td>
<td> input [@type = "checkbox" AND Emp_name = "CBD"] </td>
</tr>
<tr>
<td> Emp5 : </td>
<td> input[@type="password" AND class = "password"] </td>
</tr>
</table>
</body>
</html>
Conclusion
Returning to the multiple-element on the web page we use the multiple attributes in the expression of XPath till we get the matching element. XPath multiple attributes are written by using a web element, as per the situation we can use multiple attributes or a single attribute for finding the web page element.
Recommended Articles
This is a guide to XPath Multiple Attributes. Here we discuss the Definition, overviews, How to work XPath multiple attributes, and examples with code implementation respectively. You may also have a look at the following articles to learn more –