Bird
Raised Fist0
Selenium Pythontesting~20 mins

Getting element text in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Text Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Selenium Python code snippet?
Consider the following Selenium Python code that fetches text from a web element. What will be printed?
Selenium Python
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.webdriver import WebDriver

class DummyDriver(WebDriver):
    def find_element(self, by, value):
        class Element:
            text = "Hello World"
        return Element()

# Simulate driver
driver = DummyDriver()
element = driver.find_element(By.ID, "greeting")
print(element.text)
ANone
Bgreeting
CHello World
DBy.ID
Attempts:
2 left
💡 Hint
The text attribute of the element contains the visible text.
❓ assertion
intermediate
1:30remaining
Which assertion correctly verifies the text of a web element?
You want to check that the text of a web element is exactly 'Submit'. Which assertion statement is correct?
Selenium Python
element_text = "Submit"  # Assume this is fetched from element.text
Aassert element_text.equals('Submit')
Bassert element_text == 'Submit'
Cassert element_text is 'Submit'
Dassert element_text != 'Submit'
Attempts:
2 left
💡 Hint
Use Python's equality operator to compare strings.
❓ locator
advanced
2:00remaining
Which locator is best to get the text of a button with text 'Cancel'?
You want to locate a button element by its visible text 'Cancel' to get its text. Which locator is best?
ABy.XPATH, "//button[text()='Cancel']"
BBy.ID, "Cancel"
CBy.CLASS_NAME, "Cancel"
DBy.CSS_SELECTOR, "button:contains('Cancel')"
Attempts:
2 left
💡 Hint
Use XPath to locate elements by exact text content.
🔧 Debug
advanced
2:30remaining
Why does this code raise an AttributeError when getting element text?
Given this code snippet, why does it raise AttributeError: 'NoneType' object has no attribute 'text'?
Selenium Python
element = driver.find_element(By.ID, "nonexistent")
print(element.text)
AThe element was not found, so find_element returned None, causing the error.
BThe element has no text attribute, causing the error.
CThe driver is not initialized, causing the error.
DThe locator By.ID is invalid, causing the error.
Attempts:
2 left
💡 Hint
Check what find_element returns if no element matches.
❓ framework
expert
3:00remaining
In a pytest Selenium test, which code snippet correctly asserts the text of a web element?
You want to write a pytest test that asserts a web element's text is 'Login'. Which snippet is correct?
Selenium Python
from selenium.webdriver.common.by import By
from selenium import webdriver
import pytest

def test_login_button_text():
    driver = webdriver.Chrome()
    driver.get('https://example.com')
    element = driver.find_element(By.ID, 'login-btn')
    # Assertion here
    driver.quit()
Aassert element.getText == 'Login'
Bassert element.get_text() == 'Login'
Cassert element.text() == 'Login'
Dassert element.text == 'Login'
Attempts:
2 left
💡 Hint
Check the correct attribute or method to get text in Selenium Python.

Practice

(1/5)
1. What does the .text property do in Selenium when used on a web element?
easy
A. It retrieves the visible text inside the element on the webpage.
B. It clicks the element on the webpage.
C. It returns the HTML code of the element.
D. It clears the text inside an input field.

Solution

  1. Step 1: Understand the purpose of .text

    The .text property in Selenium is used to get the visible text content from a web element.
  2. Step 2: Compare with other actions

    Clicking, returning HTML, or clearing text are different actions and not related to .text.
  3. Final Answer:

    It retrieves the visible text inside the element on the webpage. -> Option A
  4. Quick Check:

    .text gets visible text [OK]
Hint: Remember: .text always gets visible text from element [OK]
Common Mistakes:
  • Confusing .text with .click() method
  • Expecting .text to return HTML code
  • Using .text to clear input fields
2. Which of the following is the correct syntax to get the text of a web element stored in variable element in Selenium Python?
easy
A. element.getText()
B. element.text()
C. element.get_text()
D. element.text

Solution

  1. Step 1: Recall Selenium Python syntax for text retrieval

    In Selenium Python, the visible text of an element is accessed using the property .text without parentheses.
  2. Step 2: Check other options for correctness

    getText() and get_text() are not valid in Selenium Python. text() with parentheses is incorrect because text is a property, not a method.
  3. Final Answer:

    element.text -> Option D
  4. Quick Check:

    Use property .text without parentheses [OK]
Hint: Use element.text without () to get text [OK]
Common Mistakes:
  • Adding parentheses after .text
  • Using Java-style getText() in Python
  • Using incorrect method names like get_text()
3. What will be the output of the following Selenium Python code snippet?
from selenium import webdriver
from selenium.webdriver.common.by import By

# Assume driver is already initialized and page loaded
button = driver.find_element(By.ID, "submit-btn")
print(button.text)

Assuming the button on the page shows the label "Submit Form", what will be printed?
medium
A. "submit-btn"
B. An empty string ""
C. "Submit Form"
D. Raises NoSuchElementException

Solution

  1. Step 1: Locate element and get text

    The code finds the element with ID "submit-btn" and prints its visible text using .text.
  2. Step 2: Understand what .text returns

    The visible label on the button is "Submit Form", so button.text returns this string.
  3. Final Answer:

    "Submit Form" -> Option C
  4. Quick Check:

    Element text matches visible label [OK]
Hint: Text property prints visible label, not element ID [OK]
Common Mistakes:
  • Expecting element ID instead of visible text
  • Assuming empty string if element found
  • Confusing element attribute with text
4. You wrote this code to get text from a web element:
element_text = driver.find_element(By.CLASS_NAME, "title").text()

But it raises an error. What is the problem?
medium
A. Using parentheses after .text which is a property, not a method.
B. Incorrect locator strategy; CLASS_NAME is invalid.
C. Missing import for By class.
D. The element with class "title" does not exist.

Solution

  1. Step 1: Check usage of .text property

    In Selenium Python, .text is a property and should not have parentheses. Using .text() causes a TypeError.
  2. Step 2: Verify other options

    CLASS_NAME is a valid locator, imports are assumed correct, and element existence is not guaranteed but error is about syntax here.
  3. Final Answer:

    Using parentheses after .text which is a property, not a method. -> Option A
  4. Quick Check:

    Use .text without () to avoid errors [OK]
Hint: Remember: .text is a property, no parentheses [OK]
Common Mistakes:
  • Calling .text as a method with ()
  • Confusing locator validity
  • Assuming missing imports cause this error
5. You want to verify that a paragraph with id "info" contains the exact text "Welcome to the site!". Which Selenium Python assertion is correct to check this?
hard
A. assert driver.find_element(By.ID, "info").getText() == "Welcome to the site!"
B. assert driver.find_element(By.ID, "info").text == "Welcome to the site!"
C. assert driver.find_element(By.ID, "info").text() == "Welcome to the site!"
D. assert driver.find_element(By.ID, "info").get_text == "Welcome to the site!"

Solution

  1. Step 1: Use correct property to get text

    In Selenium Python, use .text property without parentheses to get visible text.
  2. Step 2: Write assertion correctly

    The assertion compares the text property to the expected string exactly.
  3. Step 3: Identify incorrect options

    getText() and get_text are invalid in Selenium Python; text() with parentheses is wrong.
  4. Final Answer:

    assert driver.find_element(By.ID, "info").text == "Welcome to the site!" -> Option B
  5. Quick Check:

    Use .text property in assertion without () [OK]
Hint: Assert with .text property, no parentheses [OK]
Common Mistakes:
  • Using .text() with parentheses in assertion
  • Using Java-style getText() method
  • Using get_text property which doesn't exist
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