0
0
Selenium Javatesting~5 mins

XPath functions (contains, starts-with) in Selenium Java - 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 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?
Aequals()
Bstarts-with()
Cends-with()
Dcontains()
What does this XPath select? //div[contains(@class, 'active')]
AAll divs with id containing 'active'
BAll divs with class exactly 'active'
CAll divs with class containing 'active' anywhere
DAll divs with text 'active'
Which XPath expression finds inputs with id starting with 'email'?
A//input[starts-with(@id, 'email')]
B//input[@id='email']
C//input[contains(@id, 'email')]
D//input[ends-with(@id, 'email')]
Why are contains() and starts-with() useful in Selenium tests?
AThey allow flexible element matching when attributes change
BThey speed up test execution
CThey replace all locators
DThey only work with text nodes
Which is a valid XPath using contains() to find a link with href containing 'login'?
A//a[contains(text(), 'login')]
B//a[starts-with(@href, 'login')]
C//a[@href='login']
D//a[contains(@href, '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.