0
0
Selenium Javatesting~20 mins

XPath functions (contains, starts-with) in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
XPath Functions Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
locator
intermediate
2:00remaining
Locate element using contains() function
Which XPath expression correctly locates a button element whose class attribute contains the word 'submit' anywhere in its value?
A//button[@class='submit']
B//button[contains(@class, 'submit')]
C//button[contains(text(), 'submit')]
D//button[starts-with(@class, 'submit')]
Attempts:
2 left
💡 Hint
Use contains() to find attribute values that include a substring anywhere.
locator
intermediate
2:00remaining
Locate element using starts-with() function
Which XPath expression correctly locates an input element whose id attribute starts with 'user_'?
A//input[contains(@id, 'user_')]
B//input[contains(text(), 'user_')]
C//input[@id='user_']
D//input[starts-with(@id, 'user_')]
Attempts:
2 left
💡 Hint
Use starts-with() to match the beginning of an attribute value.
Predict Output
advanced
2:00remaining
XPath with contains() and attribute matching
Given the following HTML snippet:
<div class="menu-item active">Home</div>
<div class="menu-item">About</div>
<div class="menu-item active">Contact</div>

What is the number of elements matched by the XPath //div[contains(@class, 'active')]?
A0
B3
C2
D1
Attempts:
2 left
💡 Hint
Count div elements whose class attribute includes 'active'.
assertion
advanced
2:00remaining
Assertion on element text using XPath starts-with()
You want to assert that a heading element's text starts with 'Welcome'. Which XPath expression should you use in your Selenium assertion to locate this element?
A//h1[starts-with(text(), 'Welcome')]
B//h1[contains(@text, 'Welcome')]
C//h1[contains(text(), 'Welcome')]
D//h1[starts-with(@text, 'Welcome')]
Attempts:
2 left
💡 Hint
Use starts-with() on the text() node, not on an attribute.
🔧 Debug
expert
3:00remaining
Identify the error in XPath expression
Consider the following XPath expression used in Selenium:
//a[contains(@href, 'login') and starts-with(text(), 'Log')]
What is the most likely reason this XPath might fail to locate the intended element?
AThe text() function may not match if the element contains nested tags or extra whitespace.
BThe expression is correct and will always locate the element if it exists.
CThe text() function cannot be used with starts-with() in XPath.
Dcontains() cannot be combined with starts-with() in the same XPath expression.
Attempts:
2 left
💡 Hint
Consider how text() behaves with nested elements or formatting.