0
0
Selenium Pythontesting~20 mins

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

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