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()
✗ Incorrect
starts-with() checks if the attribute or text begins with the given substring.
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'
✗ Incorrect
The contains() function checks if the class attribute contains 'active' anywhere.
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')]
✗ Incorrect
starts-with() matches attributes that begin with the given string.
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
✗ 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'?
A//a[contains(text(), 'login')]
B//a[starts-with(@href, 'login')]
C//a[@href='login']
D//a[contains(@href, '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.
Practice
(1/5)
1. Which XPath function would you use to locate elements whose attribute contains a specific substring anywhere within it?
easy
A. starts-with()
B. ends-with()
C. contains()
D. equals()
Solution
Step 1: Understand the purpose of contains()
The contains() function checks if an attribute or text contains a given substring anywhere inside it.
Step 2: Compare with other functions
starts-with() checks only the beginning, ends-with() is not standard XPath 1.0, and equals() is not an XPath function.
Final Answer:
contains() -> Option C
Quick Check:
Substring anywhere = contains() [OK]
Hint: Use contains() for substring anywhere inside attribute [OK]
Common Mistakes:
Confusing contains() with starts-with()
Using ends-with() which is not standard XPath 1.0
Trying to use equals() as XPath function
2. Which of the following is the correct XPath syntax to find a button element whose id attribute starts with "submit"?
easy
A. //button[contains(@id, 'submit')]
B. //button[@id*='submit']
C. //button[@id^='submit']
D. //button[starts-with(@id, 'submit')]
Solution
Step 1: Identify the correct XPath function for 'starts with'
The function starts-with() is used to check if an attribute starts with a substring.
Step 2: Check syntax correctness
//button[starts-with(@id, 'submit')] is correct XPath syntax. Options B and C use invalid XPath operators.
Final Answer:
//button[starts-with(@id, 'submit')] -> Option D
Quick Check:
Starts with = starts-with() [OK]
Hint: Use starts-with(@attr, 'text') for prefix matching [OK]
Common Mistakes:
Using contains() instead of starts-with() for prefix
What is the result of this XPath query? //*[@id[contains(., 'user')]]
medium
A. Syntax error due to incorrect XPath function usage
B. Selects all elements with any id attribute
C. Selects the input element because its id contains 'user'
D. Selects no elements because 'user' is not in the value attribute
Solution
Step 1: Analyze the XPath syntax
The XPath //*[@id[contains(., 'user')]] is syntactically incorrect because @id is an attribute node and cannot be used as a function or have predicates applied directly like this.
Step 2: Correct XPath syntax
To check if the id attribute contains 'user', the correct XPath is //*[@id[contains(., 'user')]] is invalid; instead use //*[contains(@id, 'user')].
Final Answer:
Syntax error due to incorrect XPath function usage -> Option A
Quick Check:
Attribute predicates must be correctly formed [OK]
Hint: Use contains(@id, 'user') not @id[contains()] [OK]
Common Mistakes:
Using predicates directly on attribute nodes
Assuming . refers to attribute value in this context
Incorrect predicate syntax
4. You wrote this XPath to find links starting with "http": //a[contains(starts-with(@href, 'http'))] Why does this XPath fail to find any elements?
medium
A. Because @href attribute is missing
B. Because contains() cannot wrap starts-with() function
C. Because starts-with() requires two arguments
D. Because the syntax is correct but no links start with 'http'
Solution
Step 1: Understand function nesting rules in XPath
The contains() function expects two string arguments, but here it wraps starts-with() which returns a boolean, causing a syntax error.
Step 2: Correct usage
Use either starts-with(@href, 'http') alone or contains(@href, 'http'), but not nested.
Final Answer:
Because contains() cannot wrap starts-with() function -> Option B
Quick Check:
Do not nest contains() and starts-with() [OK]
Hint: Do not nest XPath functions like contains(starts-with()) [OK]
Common Mistakes:
Nesting contains() and starts-with() incorrectly
Assuming starts-with() returns string for contains()
Ignoring function argument requirements
5. You want to locate all <div> elements whose class attribute starts with "alert" but excludes those that contain "dismiss" anywhere in the class. Which XPath expression correctly achieves this?
hard
A. //div[starts-with(@class, 'alert') and not(contains(@class, 'dismiss'))]
B. //div[contains(@class, 'alert') and not(starts-with(@class, 'dismiss'))]
C. //div[starts-with(@class, 'alert') or not(contains(@class, 'dismiss'))]
D. //div[contains(@class, 'alert') or contains(@class, 'dismiss')]
Solution
Step 1: Use starts-with() to match class beginning
The expression starts-with(@class, 'alert') selects elements whose class attribute begins with "alert".
Step 2: Exclude elements containing 'dismiss'
The not(contains(@class, 'dismiss')) excludes elements whose class contains "dismiss" anywhere.
Step 3: Combine conditions with and
Both conditions must be true, so use and to combine them.
Final Answer:
//div[starts-with(@class, 'alert') and not(contains(@class, 'dismiss'))] -> Option A
Quick Check:
Starts with alert AND no dismiss = //div[starts-with(@class, 'alert') and not(contains(@class, 'dismiss'))] [OK]
Hint: Combine starts-with() and not(contains()) with and [OK]
Common Mistakes:
Using or instead of and causing wrong matches
Mixing contains() and starts-with() incorrectly
Forgetting to negate contains() for exclusion
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.