Updated February 14, 2023
Introduction to XPath starts-with
XPath starts-with method is used to check the starting text of an attribute, and it also finds from which element attribute will change dynamically when we refresh the page. Axes of XPath are named because they tell us about the axis element. This method is used to find the element in which attribute values are changed as per a refresh of the web page. This method is beneficial in XPath.
Overview of XPath starts-with
The XPath starts-with method will check the starting text of our attribute and find the element which will change dynamically. We use the starts-with method to find the element whose attribute will dynamically change.
Below is the syntax of the XPath starts-with method as follows:
Syntax:
XPath: //name_of_tag [starts-with (@attribute, ‘attribute_value’)]
XPath starts with the method checking whether the first string will start at the time of the second string and return the true or false value. In the above example, the name of the tag is nothing but the tag name we have defined in XPath starts-with method. We are using the name of the tag before the starts-with method. While using this method, we need to use the attribute name and attribute value parameter.
It is used to identify the element when we are familiar with the value of an attribute for a component that starts from the specified text. It will contain the elements of the dynamic web, and those elements of the webpage attribute change dynamically or are refreshed. The method was commonly used in XPath axes for a webdriver of selenium, parent sibling and child, etc. It is used to find elements based on the element’s relationship and per the elements’ document. We use multiple ways to handle complex and dynamic elements. We can use this method to handle the dynamic elements in selenium.
How to Use XPath starts with?
In the below example, we are creating the project of XPathStartsWith:
In the below example, we are making a template of the project name as XPath starts with spring boot.
1. While creating the project into the XPath starts-with using spring boot, we provide the group’s name as com.example, artifact name as XPathStartsWith, project name as XPathStartsWith, and selected java version as 11.
Group – com.example
Artifact name – XPathStartsWith
Name – XPathStartsWith
Spring boot – 2.6.7
Project – Maven
Java – 11
Package name – com.example. XPathStartsWith
Project Description – Project for XPathStartsWith
Dependencies – Spring web
Packaging – Jar
2. In the figure below, we can extract the project name as XPathStartsWith and open this project using the spring tool suite.
3. In the below step, we are checking the structure of the XPath starts-with 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 example project.
4. Below, we are adding the XPath dependency to the XPathStartsWith project. We need to add the dependency as the tag of dependency.
Code:
<dependency>
<groupId> net.sf.saxon </groupId>
<artifactId> saxon-XPath </artifactId>
<version> 8.7 </version>
</dependency>
Output:
5. Now, we are creating the file name emp.xml and adding the emp details into it to define the use of XPath starts as follows.
Code:
<?xml version="1.0" encoding="UTF-8"?>
<Employee>
<Emp>
<EmpId> 101 </EmpId>
<Name>
<EmpName> ABC </EmpName>
<Surname> XYZ </Surname>
</Name>
<Addr>
<Emp_addr> Pune </Emp_addr>
</Addr>
<DateOfBirth> 01/02/1995 </DateOfBirth>
</Emp>
<Emp>
<EmpId> 102 </EmpId>
<Name>
<EmpName> PQR </EmpName>
<Surname> CBD </Surname>
</Name>
<Addr>
<Emp_addr> Mumbai </Emp_addr>
</Addr>
<DateOfBirth> 01/02/1993 </DateOfBirth>
</Emp>
</Employee>
Output:
6. After creating the xml file, we need to create a java file. We are adding the below code into the java file as follows.
Code:
public class startswith
{
public static void main(String[] args)
{
WebServer XPathdriver = new FirefoxDriver ();
XPathdriver.manage ().window ().maximize();
String URL = "example.com";
XPathdriver.get (URL);
XPathdriver.manage ().timeouts ().implicitlyWait (15, TimeUnit.SECONDS);
WebElement searchBox = XPathdriver.findElement (By.XPath ("//input[starts-with(@name, 'keyword')]"));
searchBox.sendKeys ("Selenium WebDriver");
button = XPathdriver.findElement(By.XPath("//button[starts-with(@type,'submit')]"));
button.click ();
XPathdriver.close ();
System.out.println ("XPath starts-with");
}
}
Output:
XPath starts-with Function
To load the parse xml document using Microsoft xml is a general requirement in a document object model for identifying attribute elements whose value starts with a sequence of characters or a specific character. To define the XPath starts with function first; we create the xml document name as stud.xml as follows.
In the below example, we are defining the student information to show the example of the starts-with function.
Code:
<?xml version="1.0" encoding="UTF-8"?>
<Student>
<Stud>
<StudId> 101 </StudId>
<Name>
<StudName> ABC </StudName>
<Surname> XYZ </Surname>
</Name>
<Addr>
<Stud_addr> Pune </Stud_addr> </Addr>
<DateOfBirth> 01/02/2014 </DateOfBirth>
</Stud>
<Stud>
<StudId> 102 </StudId>
<Name>
<StudName> PQR </StudName>
<Surname> CBD </Surname>
</Name>
<Addr>
<Stud_addr> Mumbai </Stud_addr>
</Addr>
<DateOfBirth> 01/02/2013 </DateOfBirth>
</Stud>
</Student>
Output:
Then we opened the preceding code, loading the xml from the stud.xml files. It will execute the XPath query, which uses the starts-with XPath function for identifying all students whose first names will start with the A letter. We can also initiate for loop on to the selected nodes and display the first names. The function’s first parameter specifies the source or the string node, which comparison needs to be executed.
The second parameter of the XPath starts-with function is the string pattern which specifies the sequence of characters used in a comparison. In function, it is essential to remember the string pattern supplied in a second parameter with the function name as starts-with. We can run our project by using the following way. The below example shows to run the example of the starts-with function.
Conclusion
The XPath starts-with method will check the starting text of our attribute and find the element which attribute will change dynamically. The method is used to check the starting text of an attribute, and it also finds from which element attribute will change dynamically when we refresh the page.
Recommended Articles
This is a guide to XPath starts-with. Here we discuss the introduction, function, and how to use XPath starts-with. You may also have a look at the following articles to learn more –