EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials XML Tutorial XPath starts-with
 

XPath starts-with

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.

 

 

XPath starts-with

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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

XPath starts-with using spring boot

2. In the figure below, we can extract the project name as XPathStartsWith and open this project using the spring tool suite.

extract the project name as XPathStartsWith

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.

project dependency

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:

add the dependency in as tag of dependency

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:

creating the file name emp.xml

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:

we need to create java file

XPath starts-with 8

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:

defining the student information

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.

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 –

  1. XPath Sibling
  2. XPath Ancestor
  3. XPath regex
  4. XPath namespace

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW