Bird
Raised Fist0
Selenium Pythontesting~20 mins

XPath with contains and starts-with in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
XPath Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ locator
intermediate
2: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?
A//button[starts-with(@class, 'submit')]
B//button[contains(@class, 'submit')]
C//button[@class='submit']
D//button[contains(text(), 'submit')]
Attempts:
2 left
💡 Hint
contains() checks if an attribute includes a substring anywhere inside it.
❓ locator
intermediate
2:00remaining
Choose the XPath using starts-with()
Which XPath expression selects all <input> elements whose id attribute starts with 'user_'?
A//input[contains(@id, 'user_')]
B//input[contains(text(), 'user_')]
C//input[starts-with(@id, 'user_')]
D//input[@id='user_']
Attempts:
2 left
💡 Hint
starts-with() matches only if the attribute begins with the given string.
❓ assertion
advanced
2: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?
Await.until(EC.presence_of_element_located((By.XPATH, "//div[contains(@class, 'alert')]"))) # Passes if element found
Bwait.until(EC.presence_of_element_located((By.XPATH, "//div[starts-with(@class, 'alert')]"))) # Passes if element found
Cwait.until(EC.presence_of_element_located((By.XPATH, "//div[@class='alert']"))) # Passes if element found
Dwait.until(EC.presence_of_element_located((By.XPATH, "//div[contains(text(), 'alert')]"))) # Passes if element found
Attempts:
2 left
💡 Hint
Use contains() on class attribute to match any class containing 'alert'.
❓ Predict Output
advanced
2:00remaining
Output of XPath expression with contains and starts-with
Given the following HTML snippet:
<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'?
A//li[starts-with(@class, 'active')]
B//li[contains(@class, 'active')]
C//li[@class='active']
D//li[contains(text(), 'active')]
Attempts:
2 left
💡 Hint
starts-with() matches only if class attribute begins with the string.
🔧 Debug
expert
3:00remaining
Debugging XPath with contains() in Selenium test
A Selenium test uses this XPath to find a 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?
AThe XPath syntax is invalid because contains() requires two arguments but only one is given.
Bcontains() does not work with multiple classes in class attribute; must use starts-with() instead.
CThe class attribute value must be matched exactly; contains() cannot match partial strings.
DThe XPath is correct; the failure is due to the button being inside an iframe not switched to.
Attempts:
2 left
💡 Hint
Check if the element is inside an iframe or shadow DOM.

Practice

(1/5)
1. Which XPath function would you use to find an element whose id attribute contains the text "submit" anywhere inside it?
easy
A. starts-with(@id, 'submit')
B. contains(@id, 'submit')
C. ends-with(@id, 'submit')
D. equals(@id, 'submit')

Solution

  1. Step 1: Understand the contains() function

    The contains() function checks if an attribute or text includes a specific substring anywhere inside it.
  2. Step 2: Apply contains() to the id attribute

    Using contains(@id, 'submit') finds elements where the id attribute has 'submit' anywhere in its value.
  3. Final Answer:

    contains(@id, 'submit') -> Option B
  4. Quick Check:

    contains() = substring anywhere [OK]
Hint: Use contains() to match substring anywhere in attribute [OK]
Common Mistakes:
  • Using starts-with() when substring is not at start
  • Using ends-with() which is not supported in XPath 1.0
  • Using equals() which requires exact match
2. Which of the following is the correct XPath syntax to select elements whose class attribute starts with "btn-"?
easy
A. //element[@class^='btn-']
B. //element[contains(@class, 'btn-')]
C. //element[starts-with(@class, 'btn-')]
D. //element[@class*='btn-']

Solution

  1. Step 1: Identify the correct function for 'starts with'

    The starts-with() function is used to check if an attribute value begins with a specific substring.
  2. Step 2: Check XPath syntax

    The correct syntax is //element[starts-with(@class, 'btn-')]. Options C and D use invalid XPath syntax for this purpose.
  3. Final Answer:

    //element[starts-with(@class, 'btn-')] -> Option C
  4. Quick Check:

    starts-with() = prefix match [OK]
Hint: Use starts-with() for prefix matching in XPath [OK]
Common Mistakes:
  • Using contains() instead of starts-with() for prefix
  • Using CSS selector syntax like ^= or *= in XPath
  • Missing parentheses in function call
3. What will be the result of this XPath query?
//div[contains(text(), 'Error') and starts-with(@class, 'alert-')]

Assuming the HTML contains:
<div class="alert-danger">Error: Invalid input</div>
<div class="alert-success">Success</div>
<div class="message-error">Error occurred</div>
medium
A. Selects only the div with class 'alert-danger'
B. Selects no elements
C. Selects all div elements containing 'Error'
D. Selects the first and second div elements

Solution

  1. Step 1: Analyze contains(text(), 'Error')

    This matches div elements whose visible text includes 'Error'. The first and third divs contain 'Error'.
  2. Step 2: Analyze starts-with(@class, 'alert-')

    This matches div elements whose class attribute starts with 'alert-'. The first and second divs match this.
  3. Step 3: Combine both conditions with AND

    Only the first div satisfies both: text contains 'Error' and class starts with 'alert-'.
  4. Final Answer:

    Selects only the div with class 'alert-danger' -> Option A
  5. Quick Check:

    AND filters to first div only [OK]
Hint: AND requires both conditions true for selection [OK]
Common Mistakes:
  • Assuming contains() matches attribute instead of text
  • Ignoring the starts-with() condition
  • Selecting all divs with 'Error' ignoring class prefix
4. You wrote this XPath to find buttons starting with "save":
//button[contains(@id, 'save-')]

But it also matches buttons with id "disable-save". How to fix it?
medium
A. //button[starts-with(@id, 'save-')]
B. //button[contains(text(), 'save-')]
C. //button[@id='save-']
D. //button[ends-with(@id, 'save-')]

Solution

  1. Step 1: Understand why contains() matches unwanted ids

    contains(@id, 'save-') matches any id containing 'save-' anywhere, including 'disable-save'.
  2. 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.
  3. Final Answer:

    //button[starts-with(@id, 'save-')] -> Option A
  4. 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

  1. Step 1: Match href attribute starting with 'https://'

    Use starts-with(@href, 'https://') to ensure href begins exactly with 'https://'.
  2. Step 2: Match link text containing 'Learn'

    Use contains(text(), 'Learn') to find links whose visible text includes 'Learn' anywhere.
  3. Step 3: Combine both conditions with AND

    Both conditions must be true, so use and between them.
  4. Final Answer:

    //a[starts-with(@href, 'https://') and contains(text(), 'Learn')] -> Option D
  5. Quick Check:

    starts-with() + contains() combined correctly [OK]
Hint: Combine starts-with() for attribute and contains() for text [OK]
Common Mistakes:
  • Using contains() for href prefix instead of starts-with()
  • Swapping text() and attribute in functions
  • Using starts-with() on text when contains() is needed
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