Challenge - 5 Problems
XPath Attribute Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ locator
intermediate2:00remaining
Identify the correct XPath to locate a button by its attribute
Given a button element with
type='submit' and class='btn-primary', which XPath expression correctly locates this button using both attributes?Selenium Python
<button type="submit" class="btn-primary">Submit</button>
Attempts:
2 left
💡 Hint
Use
and to combine multiple attribute conditions in XPath.✗ Incorrect
XPath uses
and to combine multiple attribute conditions. Option D correctly uses and. Option D uses or, which matches elements with either attribute, not both. Option D is missing a logical operator, causing syntax error. Option D uses &&, which is invalid in XPath.❓ assertion
intermediate2:00remaining
Assertion on element text using XPath with attribute
You want to assert that a
div with attribute data-status='active' contains the text "Active User". Which assertion code snippet using Selenium in Python is correct?Selenium Python
element = driver.find_element(By.XPATH, "//div[@data-status='active']") actual_text = element.text # Assertion here
Attempts:
2 left
💡 Hint
Use Python's equality operator for assertions.
✗ Incorrect
Option A correctly uses Python's equality operator == for assertion. Option A uses assignment = inside assert, causing SyntaxError. Option A uses Java-style equals method, invalid in Python. Option A asserts inequality, which is opposite of the requirement.
❓ Predict Output
advanced2:00remaining
Output of XPath locator count with attribute filter
What is the output of the following Selenium Python code snippet if the page contains 3
input elements with type='checkbox' and 2 with type='radio'?Selenium Python
checkboxes = driver.find_elements(By.XPATH, "//input[@type='checkbox']") print(len(checkboxes))
Attempts:
2 left
💡 Hint
XPath filters elements by attribute value exactly.
✗ Incorrect
The XPath selects only
input elements with type='checkbox'. There are 3 such elements, so output is 3. Option C counts all inputs, which is incorrect. Option C counts radios only. Option C is zero, which is wrong.🔧 Debug
advanced2:00remaining
Identify the error in XPath with attribute syntax
Which option contains an XPath expression that will cause a syntax error when used in Selenium?
Attempts:
2 left
💡 Hint
Look for incomplete logical operators in XPath.
✗ Incorrect
Option B ends with
or without a second condition, causing XPath syntax error. Options B, C, and D are valid XPath expressions.❓ framework
expert3:00remaining
Best practice for locating elements with dynamic attributes in Selenium
In a web app, the
id attribute of a button changes dynamically but always contains the substring submit-btn. Which XPath locator is best to reliably find this button?Attempts:
2 left
💡 Hint
Use XPath functions to match partial attribute values.
✗ Incorrect
Option A uses
contains() to match any id containing 'submit-btn', handling dynamic parts. Option A matches exact id, which fails if dynamic. Option A matches only if id starts with 'submit-btn', which may fail if substring is in middle. Option A matches any button with id, too broad.