XPath Practice: Mastering XPath Expressions for Effective Web Automation using Contains() Function

Test Your Skills with this Fun and Interactive Game


Home->Practice 2

Next
HTML Source document
         <h1>Heading 1 </h1>
 <h2>Heading 2 </h2>
 <h3>Heading 3 </h3>
 <h4>Heading 4 </h4>
 <h5>Heading 5 </h5>
 <h6>Heading 6 </h6> 
        
XPath Exercise 2

Q1. Find Heading 1

Q2. Find Heading 2

Q3. Find Heading 3

Q4. Find Heading 4

Q5. Find Heading 5

Q6. Find Heading 6

Q7. Find Heading 1 using "Contains() function"

Q8. Find Heading 2 using "Contains() function"

Q9. Find Heading 3 using "Contains() function"

Q10. Find Heading 4 using "Contains() function"

Q11. Find Heading 5 using "Contains() function"

Q12. Find Heading 6 using "Contains() function"

XPath O/P

Enter XPath Input here:   

Tips and Syntax For This Page Xpath Exercise :

Using Double Slash

Syntax : //tagname

Where tagname is the name of the HTML element you want to search for.

Here are a few examples to demonstrate how to use the double slash in XPath:

e.g :
1) //p
2) //form/div[1]/div/div[1]/div/div/input[1]

Using Contains()

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

or

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

Here are a few examples to demonstrate how to use the Contains() in Selenium:

WebDriver driver = new FirefoxDriver();
driver.get("http://www.example.com");
WebElement link = driver.findElement(By.xpath("//a[contains(text(), 'Link Text')]"));
link.click();

In this example, the contains function is used to match a link whose text contains the string "Link Text". The resulting WebElement is then clicked, which simulates a user clicking the link on the page.

We would love to hear your thoughts, suggestions, concerns or problems with anything so we can improve. Request you to please use Social Media button in Right Side for this website growth.