0
0
Selenium Pythontesting~20 mins

XPath with attributes in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
XPath Attribute Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
locator
intermediate
2: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>
A//button[@type='submit' @class='btn-primary']
B//button[@type='submit' && @class='btn-primary']
C//button[@type='submit' or @class='btn-primary']
D//button[@type='submit' and @class='btn-primary']
Attempts:
2 left
💡 Hint
Use and to combine multiple attribute conditions in XPath.
assertion
intermediate
2: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
Aassert actual_text == "Active User"
Bassert actual_text = "Active User"
Cassert actual_text.equals("Active User")
Dassert actual_text != "Active User"
Attempts:
2 left
💡 Hint
Use Python's equality operator for assertions.
Predict Output
advanced
2: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))
A2
B5
C3
D0
Attempts:
2 left
💡 Hint
XPath filters elements by attribute value exactly.
🔧 Debug
advanced
2: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?
A//input[@name='email' and @required]
B//div[@class='container' or]
C//a[@href='https://example.com']
D//span[@data-id='123']
Attempts:
2 left
💡 Hint
Look for incomplete logical operators in XPath.
framework
expert
3: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?
A//button[contains(@id, 'submit-btn')]
B//button[@id='submit-btn']
C//button[starts-with(@id, 'submit-btn')]
D//button[@id]
Attempts:
2 left
💡 Hint
Use XPath functions to match partial attribute values.