Recall & Review
beginner
What does the XPath function
contains() do?The
contains() function checks if an attribute or text contains a specific substring. It helps find elements with partial matches.Click to reveal answer
beginner
How does
starts-with() differ from contains() in XPath?starts-with() checks if an attribute or text begins with a specific substring, while contains() checks if it appears anywhere inside.Click to reveal answer
beginner
Write an XPath using <code>contains()</code> to find a button with class containing 'submit'.XPath: <code>//button[contains(@class, 'submit')]</code> finds any button whose class attribute includes 'submit' anywhere.Click to reveal answer
beginner
Write an XPath using
starts-with() to find an input whose id starts with 'user'.XPath:
//input[starts-with(@id, 'user')] finds input elements with id attributes beginning with 'user'.Click to reveal answer
intermediate
Why use
contains() or starts-with() instead of exact matches in XPath?They make locators flexible and robust. Web elements often have dynamic or partial attribute values, so these functions help find elements even if the full value changes.
Click to reveal answer
Which XPath function checks if an attribute starts with a specific string?
✗ Incorrect
starts-with() checks if the attribute or text begins with the given substring.What does this XPath select?
//div[contains(@class, 'active')]✗ Incorrect
The
contains() function checks if the class attribute contains 'active' anywhere.Which XPath expression finds inputs with id starting with 'email'?
✗ Incorrect
starts-with() matches attributes that begin with the given string.Why are
contains() and starts-with() useful in Selenium tests?✗ Incorrect
These functions help locate elements even if attribute values partially change or are dynamic.
Which is a valid XPath using
contains() to find a link with href containing 'login'?✗ Incorrect
contains(@href, 'login') finds links whose href attribute includes 'login'.Explain how to use
contains() and starts-with() in XPath locators with examples.Think about partial matching of attributes or text.
You got /5 concepts.
Describe a scenario in Selenium testing where using
starts-with() or contains() is better than exact attribute matching.Consider web pages with changing element attributes.
You got /4 concepts.