0
0
Selenium Pythontesting~5 mins

XPath with contains and starts-with in Selenium Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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>&lt;button&gt;</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?
Acontains()
Bstarts-with()
Cends-with()
Dequals()
What does starts-with(@id, 'btn') select?
AElements with id exactly 'btn'
BElements with id starting with 'btn'
CElements with id containing 'btn' anywhere
DElements with id ending with 'btn'
Which XPath is correct to find a div with class containing 'active'?
A//div[contains(text(), 'active')]
B//div[starts-with(@class, 'active')]
C//div[@class='active']
D//div[contains(@class, 'active')]
Why prefer contains() over exact match in dynamic web pages?
ABecause attribute values may change slightly
BBecause exact matches find more elements
CBecause contains() is deprecated
DBecause exact matches are faster
Which is NOT a valid XPath function?
Acontains()
Bstarts-with()
Cends-with()
Dtext()
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.