Challenge - 5 Problems
XPath Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ locator
intermediate2:00remaining
Identify the correct XPath using contains()
Which XPath expression correctly selects all <button> elements whose class attribute contains the word 'submit' anywhere in the string?
Attempts:
2 left
💡 Hint
contains() checks if an attribute includes a substring anywhere inside it.
✗ Incorrect
Option B uses contains() on the class attribute, which matches any button with 'submit' inside its class string. Option B only matches if class starts exactly with 'submit'. Option B matches only if class equals 'submit' exactly. Option B checks button text, not class attribute.
❓ locator
intermediate2:00remaining
Choose the XPath using starts-with()
Which XPath expression selects all <input> elements whose id attribute starts with 'user_'?
Attempts:
2 left
💡 Hint
starts-with() matches only if the attribute begins with the given string.
✗ Incorrect
Option C correctly uses starts-with() on the id attribute to select inputs whose id begins with 'user_'. Option C matches anywhere in id, not just start. Option C matches exact id 'user_'. Option C checks text content, not id attribute.
❓ assertion
advanced2:30remaining
Assertion for element presence using XPath with contains()
You want to assert that a <div> with a class containing 'alert' is present on the page. Which assertion code snippet using Selenium WebDriver in Python is correct?
Selenium Python
from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC wait = WebDriverWait(driver, 10) # Which assertion is correct?
Attempts:
2 left
💡 Hint
Use contains() on class attribute to match any class containing 'alert'.
✗ Incorrect
Option A waits until a div with class containing 'alert' is present, which matches the requirement. Option A uses starts-with which is more restrictive. Option A matches exact class 'alert' only. Option A checks text content, not class attribute.
❓ Predict Output
advanced2:00remaining
Output of XPath expression with contains and starts-with
Given the following HTML snippet:
Which XPath expression selects exactly the <li> elements with class starting with 'active'?
<ul> <li id="item1" class="active highlight">First</li> <li id="item2" class="inactive highlight">Second</li> <li id="item3" class="active lowlight">Third</li> </ul>
Which XPath expression selects exactly the <li> elements with class starting with 'active'?
Attempts:
2 left
💡 Hint
starts-with() matches only if class attribute begins with the string.
✗ Incorrect
Option A selects li elements whose class attribute starts exactly with 'active' (matches item1 and item3). Option A selects li elements whose class contains 'active' anywhere (also item1 and item3). However, item3's class is 'active lowlight' which starts with 'active', so both A and B select same elements. But since question asks for 'starting with', A is the precise answer. Option A matches exact class 'active' only (no match). Option A checks text content, not class.
🔧 Debug
expert3:00remaining
Debugging XPath with contains() in Selenium test
A Selenium test uses this XPath to find a button:
But it fails to find the button even though the button's class attribute is 'btn btn-primary large'.
What is the most likely reason the XPath does not find the button?
//button[contains(@class, 'btn-primary')]
But it fails to find the button even though the button's class attribute is 'btn btn-primary large'.
What is the most likely reason the XPath does not find the button?
Attempts:
2 left
💡 Hint
Check if the element is inside an iframe or shadow DOM.
✗ Incorrect
Option D is correct because the XPath is valid and should find the button if it is in the current DOM context. If the button is inside an iframe, Selenium must switch to that iframe first. Option D is false; contains() works fine with class attributes containing multiple classes. Option D is false; contains() matches substrings. Option D is false; the XPath syntax is correct with two arguments.