Bird
Raised Fist0
Selenium Pythontesting~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the XPath expression //input[@type='submit'] select on a web page?
easy
A. All <input> elements with attribute type equal to 'submit'
B. All elements with tag name 'submit'
C. All elements with attribute 'input' and value 'submit'
D. All <submit> elements inside <input> tags

Solution

  1. Step 1: Understand XPath syntax

    The expression //input selects all <input> elements anywhere in the document.
  2. Step 2: Apply attribute filter

    The part [@type='submit'] filters those <input> elements to only those whose 'type' attribute equals 'submit'.
  3. Final Answer:

    All <input> elements with attribute type equal to 'submit' -> Option A
  4. Quick Check:

    XPath //tag[@attr='value'] selects elements by attribute [OK]
Hint: Look for tagname and attribute=value inside square brackets [OK]
Common Mistakes:
  • Confusing tag name with attribute name
  • Ignoring the attribute filter
  • Assuming it selects elements by text content
2. Which of the following is the correct XPath syntax to select a button element with id 'loginBtn'?
easy
A. //button[@id='loginBtn']
B. //button[id='loginBtn']
C. //button(#id='loginBtn')
D. //button[id="loginBtn"]

Solution

  1. Step 1: Recall correct attribute syntax in XPath

    Attributes are selected using @attribute='value' inside square brackets.
  2. Step 2: Check each option

    //button[@id='loginBtn'] uses @id='loginBtn' which is correct. //button[id='loginBtn'] misses '@'. //button(#id='loginBtn') uses invalid syntax. //button[id="loginBtn"] also misses '@' before the attribute name.
  3. Final Answer:

    //button[@id='loginBtn'] -> Option A
  4. Quick Check:

    Use @ before attribute name in XPath filters [OK]
Hint: Always prefix attribute with @ in XPath filters [OK]
Common Mistakes:
  • Omitting @ before attribute name
  • Using parentheses instead of square brackets
  • Incorrect quoting of attribute values
3. Given the HTML snippet:
<input type='checkbox' name='subscribe' checked>
What will the XPath //input[@checked] select?
medium
A. No elements, because 'checked' has no value
B. All <input> elements with attribute 'checked' equal to 'true'
C. All <input> elements with attribute 'checked' equal to 'checked'
D. All <input> elements with attribute 'checked' present

Solution

  1. Step 1: Understand attribute presence in XPath

    The XPath [@checked] selects elements where the 'checked' attribute exists, regardless of its value.
  2. Step 2: Apply to given HTML

    The input element has 'checked' attribute present (even without value), so it matches.
  3. Final Answer:

    All <input> elements with attribute 'checked' present -> Option D
  4. Quick Check:

    XPath [@attr] checks attribute presence, not value [OK]
Hint: Use [@attr] to check attribute exists, value not needed [OK]
Common Mistakes:
  • Assuming attribute must have a value
  • Confusing attribute presence with attribute value
  • Thinking empty attributes are ignored
4. You wrote this XPath to find a link: //a[@href='https://example.com'] but it fails to find the element. What is the likely error?
medium
A. The tag name 'a' is wrong, should be 'link'
B. XPath syntax is invalid because of missing @
C. The href attribute value is incorrect or incomplete
D. XPath cannot use attribute filters

Solution

  1. Step 1: Verify XPath syntax correctness

    The XPath //a[@href='https://example.com'] is syntactically correct with proper attribute filter.
  2. Step 2: Consider attribute value correctness

    If the element is not found, likely the href attribute value in the page differs (e.g., missing trailing slash, different protocol, or relative URL).
  3. Final Answer:

    The href attribute value is incorrect or incomplete -> Option C
  4. Quick Check:

    Check exact attribute values when XPath finds no elements [OK]
Hint: Verify exact attribute values match in XPath and page [OK]
Common Mistakes:
  • Assuming XPath syntax error without checking value
  • Using wrong tag name for links
  • Believing XPath cannot filter by attributes
5. You want to select all <input> elements that have a name attribute starting with 'user' and a type attribute exactly equal to 'text'. Which XPath expression is correct?
hard
A. //input[@name^='user' and @type='text']
B. //input[starts-with(@name, 'user') and @type='text']
C. //input[@name*='user' and @type='text']
D. //input[contains(@name, 'user') and @type='text']

Solution

  1. Step 1: Identify XPath function for 'starts with'

    XPath uses starts-with(@attr, 'value') to check if attribute starts with a string.
  2. Step 2: Combine conditions with 'and'

    Both conditions must be true: name starts with 'user' and type equals 'text'. //input[starts-with(@name, 'user') and @type='text'] correctly uses and to combine.
  3. Step 3: Evaluate other options

    //input[@name^='user' and @type='text'] uses invalid syntax @name^='user'. //input[@name*='user' and @type='text'] uses *= which is not valid XPath. //input[contains(@name, 'user') and @type='text'] uses contains() which matches anywhere, not just start.
  4. Final Answer:

    //input[starts-with(@name, 'user') and @type='text'] -> Option B
  5. Quick Check:

    Use starts-with() for prefix match in XPath [OK]
Hint: Use starts-with() for attribute prefix matching in XPath [OK]
Common Mistakes:
  • Using CSS-like ^= or *= operators in XPath
  • Confusing contains() with starts-with()
  • Not combining multiple conditions properly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard