Bird
Raised Fist0
Selenium Pythontesting~20 mins

Find element by XPath 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 to select a button with text 'Submit'
Which XPath expression correctly selects a button element with the exact visible text 'Submit'?
A//button[text()='Submit']
B//button[contains(text(),'Submit')]
C//button[@name='Submit']
D//button[@value='Submit']
Attempts:
2 left
💡 Hint
Look for an XPath that matches the exact text inside the button element.
❓ assertion
intermediate
2:00remaining
Check if element found by XPath is visible
Given the Selenium code below, what assertion correctly verifies the element found by XPath is visible on the page?
Selenium Python
element = driver.find_element(By.XPATH, "//div[@id='content']")
Aassert element is None
Bassert element.is_displayed()
Cassert element.text == ''
Dassert element.is_enabled()
Attempts:
2 left
💡 Hint
Visibility means the element is shown on the page, not just present in the HTML.
❓ Predict Output
advanced
2:00remaining
Output of Selenium find_elements with XPath returning multiple matches
What is the output type of the following Selenium Python code snippet if the XPath matches three elements on the page?
Selenium Python
elements = driver.find_elements(By.XPATH, "//ul[@id='menu']/li")
print(type(elements))
print(len(elements))
A<class 'list'>\n1
B<class 'WebElement'>\n1
C<class 'WebElement'>\n3
D<class 'list'>\n3
Attempts:
2 left
💡 Hint
find_elements returns a list of elements, even if multiple are found.
🔧 Debug
advanced
2:00remaining
Identify the error in XPath expression causing NoSuchElementException
Why does the following Selenium code raise a NoSuchElementException?
Selenium Python
element = driver.find_element(By.XPATH, "//div[@class='container'][@id='main']")
AXPath syntax error: multiple predicates should be combined with 'and' inside one []
BThe element does not exist on the page
CMissing quotes around attribute values
DUsing find_element instead of find_elements causes the error
Attempts:
2 left
💡 Hint
Check how multiple attribute conditions are combined in XPath.
❓ framework
expert
2:00remaining
Best practice for waiting for element by XPath in Selenium Python
Which code snippet correctly waits up to 10 seconds for an element located by XPath to be visible before interacting with it?
Aelement = driver.find_element(By.XPATH, "//input[@name='email']"); assert element.is_displayed()
Belement = driver.find_element(By.XPATH, "//input[@name='email']"); time.sleep(10)
Celement = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//input[@name='email']")))
Delement = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//input[@name='email']")))
Attempts:
2 left
💡 Hint
Waiting for visibility ensures the element can be interacted with, not just present in the DOM.

Practice

(1/5)
1. What does the following Selenium Python code do?
driver.find_element(By.XPATH, "//button[@id='submit']")
easy
A. Finds the button element by its class name 'submit'
B. Finds all button elements with id 'submit' on the page
C. Clicks the button with id 'submit'
D. Finds the first button element with id 'submit' on the page

Solution

  1. Step 1: Understand the method used

    The method find_element returns the first matching element found by the locator.
  2. Step 2: Analyze the XPath expression

    The XPath //button[@id='submit'] selects button elements with attribute id='submit'.
  3. Final Answer:

    Finds the first button element with id 'submit' on the page -> Option D
  4. Quick Check:

    find_element + XPath with id attribute = first matching element [OK]
Hint: find_element finds only the first matching element [OK]
Common Mistakes:
  • Confusing find_element with find_elements
  • Thinking XPath finds by class instead of id
  • Assuming it clicks the element automatically
2. Which of the following is the correct syntax to find an element by XPath in Selenium Python?
easy
A. driver.find_element(By.XPATH, "//div[@class='header']")
B. driver.find_element_by_xpath("//div[@class='header']")
C. driver.find_element(By.CSS_SELECTOR, "//div[@class='header']")
D. driver.find_element(By.ID, "//div[@class='header']")

Solution

  1. Step 1: Recall the modern Selenium syntax

    Since Selenium 4, the recommended way is driver.find_element(By.XPATH, xpath_string).
  2. Step 2: Check each option

    driver.find_element(By.XPATH, "//div[@class='header']") uses the correct syntax. driver.find_element_by_xpath("//div[@class='header']") is deprecated. Options C and D use wrong locator types for XPath.
  3. Final Answer:

    driver.find_element(By.XPATH, "//div[@class='header']") -> Option A
  4. Quick Check:

    Use By.XPATH with find_element [OK]
Hint: Use By.XPATH with find_element, not deprecated methods [OK]
Common Mistakes:
  • Using deprecated find_element_by_xpath method
  • Mixing locator types like CSS_SELECTOR with XPath
  • Passing XPath as ID locator
3. What will be the output of this code snippet if the page contains exactly one <input name='search'> element?
element = driver.find_element(By.XPATH, "//input[@name='search']")
print(element.tag_name)
medium
A. input
B. search
C. NoSuchElementException
D. button

Solution

  1. Step 1: Understand what find_element returns

    It returns the first matching WebElement for the XPath.
  2. Step 2: Check the tag_name property

    The element found is an input tag, so element.tag_name returns "input".
  3. Final Answer:

    input -> Option A
  4. Quick Check:

    XPath finds input element, tag_name returns 'input' [OK]
Hint: tag_name returns the HTML tag of the found element [OK]
Common Mistakes:
  • Confusing attribute value with tag name
  • Expecting exception when element exists
  • Assuming tag_name returns attribute value
4. What is wrong with this code snippet?
element = driver.find_element(By.XPATH, "//div[@class='content']")
print(element.text)

Assume the page has multiple <div class='content'> elements.
medium
A. It will raise NoSuchElementException
B. It only finds the first matching element, ignoring others
C. XPath syntax is incorrect
D. element.text will return None

Solution

  1. Step 1: Understand find_element behavior

    find_element returns only the first matching element, even if multiple exist.
  2. Step 2: Analyze the impact

    If multiple div.content exist, only the first is found and printed, others ignored.
  3. Final Answer:

    It only finds the first matching element, ignoring others -> Option B
  4. Quick Check:

    find_element returns first match only [OK]
Hint: Use find_elements to get all matches, find_element gets first only [OK]
Common Mistakes:
  • Expecting find_element to return all matches
  • Thinking XPath syntax is wrong
  • Assuming element.text returns None if multiple elements exist
5. You want to find a button element with text exactly 'Submit' using XPath in Selenium Python. Which XPath expression is correct to use with driver.find_element(By.XPATH, xpath)?
hard
A. //button[text()!='Submit']
B. //button[contains(text(), 'Submit')]
C. //button[text()='Submit']
D. //button[@text='Submit']

Solution

  1. Step 1: Understand XPath text() function

    XPath text()='Submit' matches elements whose text content is exactly 'Submit'.
  2. Step 2: Evaluate each option

    //button[text()='Submit'] matches exact text. //button[contains(text(), 'Submit')] matches partial text (not exact). //button[@text='Submit'] is invalid because text is not an attribute. //button[text()!='Submit'] matches elements whose text is not 'Submit'.
  3. Final Answer:

    //button[text()='Submit'] -> Option C
  4. Quick Check:

    Use text()='value' for exact text match in XPath [OK]
Hint: Use text()='value' for exact text match in XPath [OK]
Common Mistakes:
  • Using @text instead of text() function
  • Using contains() when exact match needed
  • Using != in XPath for positive match
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