0
0
Selenium Pythontesting~20 mins

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

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