contains(@id, 'save-') matches any id containing 'save-' anywhere, including 'disable-save'.
Step 2: Use starts-with() to match only ids beginning with 'save-'
starts-with(@id, 'save-') ensures only ids starting exactly with 'save-' are matched.
Final Answer:
//button[starts-with(@id, 'save-')] -> Option A
Quick Check:
starts-with() fixes unwanted matches [OK]
Hint: Use starts-with() to avoid partial matches inside strings [OK]
Common Mistakes:
Using contains() when prefix match is needed
Using ends-with() which XPath 1.0 does not support
Matching text() instead of attribute
5. You want to select all <a> elements where the href attribute starts with "https://" and the link text contains "Learn". Which XPath expression correctly does this?
hard
A. //a[contains(@href, 'https://') and contains(text(), 'Learn')]
B. //a[contains(text(), 'https://') and starts-with(@href, 'Learn')]
C. //a[starts-with(text(), 'Learn') and contains(@href, 'https://')]
D. //a[starts-with(@href, 'https://') and contains(text(), 'Learn')]
Solution
Step 1: Match href attribute starting with 'https://'
Use starts-with(@href, 'https://') to ensure href begins exactly with 'https://'.
Step 2: Match link text containing 'Learn'
Use contains(text(), 'Learn') to find links whose visible text includes 'Learn' anywhere.
Step 3: Combine both conditions with AND
Both conditions must be true, so use and between them.
Final Answer:
//a[starts-with(@href, 'https://') and contains(text(), 'Learn')] -> Option D