XPath Contains(), Text(), Starts-With(): Enhance your skills by practicing XPath Contains(), Text(), Starts-With() syntax with our given sample HTML codes and interactive exercises, designed to help you master selecting elements in HTML and XML documents.

Boost Your Skills: Practice XPath Syntax with easy Exercises.


Home → Practice 3

Next Exercise >>
Step 1: Read XPath Question → Step 2: Check HTML Source Code → Step 3: Enter XPath Syntax in O/P Box

XPath Exercise 3

Q1. Find Contact US using Text()

Q2. Find Contact US using Contains()

Q3. Find Contact US using Starts-With()

Q4. Find Para 1 using ID attribute

Q5. Find Para 1 using Class attribute

Q6. Find Para 1 using ID and Class attribute

Q7. Find all Para inside the DIV tag

Q8. Find Para 2 using Style attribute

Q9. Find Para 3 using Style attribute

Q10. Find Para 4 using Style attribute

Q11. Find Para 2 and 3 together

Q12. Find Para 3 and 4 together

Q13. Find All Para

HTML Source Document

<span class="sub-text">
  <p>Contact US</p>
</span>

<p id="p1" class="para">Para 1</p>

<div class="container">
  <p id="p2" class="para" style="left:252px;">Para 2</p>
  <p id="p3" class="para" style="width:117px;">Para 3</p>
  <p id="p4" class="para" style="height:24px;">Para 4</p>
</div>
      

XPath O/P Box

Enter XPath Syntax here:

XPath Tips and Syntax For This Page Exercise:

Using Contains()

Contains() is useful for identifying elements with dynamic attributes or partial matches.

//HTML tag[contains(@attribute_name,'attribute_value')]
or
//*[contains(@attribute_name,'attribute_value')]
    

Using text()

Selects the text content of an element.

//*[text()='testdata.com']
    

Using starts-with()

//HTMLtag[starts-with(@attribute_name,'attribute_value')]
or
//*[starts-with(@attribute_name,'attribute_value')]

Example:
driver.get("https://www.example.com");
List elements = driver.findElements(By.xpath("//*[starts-with(@class, 'example-')]"));
    

Using ID, Class, Style Attribute

//HTMLtag[@attribute_name='attribute_value']
or
//*[@attribute_name='attribute_value']
    

Using AND

//input[@id='Email' and @name='Email']
or
//*[@id='Email' and @name='Email']
    
We would love to hear your thoughts, suggestions, concerns or problems with anything so we can improve. Please use the Social Media button on the right side to help this website grow.