Verifying Text on a Web Page with Selenium Python
When automating web applications with Selenium in Python, one of the most common tasks is verifying text on a web page. Whether you're confirming a login success message, checking product titles, or validating form errors, verifying text ensures your application behaves as expected.
In this blog, we’ll explore different ways to verify text using Selenium with Python, along with practical examples and tips for making your test scripts more reliable.
๐งฐ Prerequisites
- Before diving in, ensure you have the following installed:
- Python 3.x
- Selenium
- WebDriver for your browser (e.g., ChromeDriver)
Install Selenium using pip:
bash
Copy
Edit
pip install selenium
๐ Why Text Verification Matters
Text verification is essential for:
Validating UI behavior: Checking messages, labels, or dynamic content.
Test pass/fail logic: Determining whether a test should succeed or fail.
Ensuring correct user experience: Making sure users see the right information.
✅ Basic Example: Verifying Static Text
Let’s start with a basic example where we verify a welcome message on a login page.
python
Copy
Edit
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://example.com/login")
# Perform login steps here...
# Locate the text element
welcome_text = driver.find_element(By.ID, "welcome-msg").text
# Verify the expected text
assert welcome_text == "Welcome, John!", "Text does not match!"
driver.quit()
Here, we:
Open a browser with webdriver.Chrome()
Locate the element using By.ID
Extract the visible text using .text
Use an assert statement to verify it matches the expected value
๐ Verifying Partial or Dynamic Text
Sometimes, the text may contain dynamic parts (like usernames or timestamps). You can use Python's in keyword:
python
Copy
Edit
message = driver.find_element(By.CLASS_NAME, "alert").text
assert "Login successful" in message, "Login message not found!"
This checks if a particular substring exists within the text.
⏳ Using Waits for Dynamic Content
If the text appears after a delay, use WebDriverWait to avoid race conditions:
python
Copy
Edit
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
element = wait.until(EC.visibility_of_element_located((By.ID, "status-msg")))
assert "Completed" in element.text
This waits up to 10 seconds for the element to be visible before verifying its text.
๐ Tips for Reliable Text Verification
Trim whitespace:
python
Copy
Edit
text = element.text.strip()
Be case-insensitive:
python
Copy
Edit
assert "success" in text.lower()
Avoid brittle locators: Use stable attributes like data-testid, id, or name.
Handle special characters: Normalize text using .strip() or .replace() if needed.
๐งช Common Use Cases
Error validation:
python
Copy
Edit
error = driver.find_element(By.CSS_SELECTOR, ".error-msg").text
assert error == "Invalid email address."
Table data verification:
python
Copy
Edit
cell_text = driver.find_element(By.XPATH, "//table/tbody/tr[1]/td[2]").text
assert cell_text == "Pending"
Alert message check:
python
Copy
Edit
alert = driver.switch_to.alert.text
assert "Are you sure?" in alert
✅ Conclusion
Verifying text with Selenium in Python is a fundamental part of web UI testing. By using the right locating strategies, smart assertions, and proper wait conditions, you can write test scripts that are both robust and effective.
Learn Selenium with Pyhton Training Hyderabad
Read More: Capturing Page Titles and URLs in Selenium Python
Get Direction
Comments
Post a Comment