Recall & Review
beginner
What does the XPath function
contains() do?The
contains() function checks if an element's attribute or text contains a specific substring. It helps find elements partially matching a value.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 the substring appears anywhere.Click to reveal answer
beginner
Write an XPath using <code>contains()</code> to find a button with class containing 'submit'.<code>//button[contains(@class, 'submit')]</code> finds any <code><button></code> element whose class attribute includes 'submit' anywhere.Click to reveal answer
beginner
Write an XPath using
starts-with() to find an input with id starting with 'user'.//input[starts-with(@id, 'user')] finds any <input> element whose id attribute starts exactly with 'user'.Click to reveal answer
intermediate
Why use
contains() or starts-with() instead of exact matches in XPath?They help locate elements when attribute values change slightly or have dynamic parts, making tests more flexible and less brittle.
Click to reveal answer
Which XPath function finds elements whose attribute contains a substring anywhere?
✗ Incorrect
contains() checks if the attribute or text includes the substring anywhere.What does
starts-with(@id, 'btn') select?✗ Incorrect
starts-with() matches attributes that begin with the given substring.Which XPath is correct to find a div with class containing 'active'?
✗ Incorrect
contains(@class, 'active') finds any div whose class includes 'active' anywhere.Why prefer
contains() over exact match in dynamic web pages?✗ Incorrect
Dynamic pages may change attribute values, so partial matching with
contains() is more reliable.Which is NOT a valid XPath function?
✗ Incorrect
ends-with() is not a standard XPath function; use other methods to simulate it.Explain how to use
contains() and starts-with() in XPath to locate web elements.Think about partial matching of attribute values.
You got /4 concepts.
Describe why using
contains() or starts-with() can make Selenium tests more robust.Consider how web pages change over time.
You got /3 concepts.