How to Check if Some Text is Present on a Web Page Using Selenium 2

How to Check if Some Text is Present on a Web Page Using Selenium 2

21 May 2024 Stephan Petzl Leave a comment Tech-Help

When automating tests on web pages using Selenium 2 and Python, you may encounter scenarios where you need to verify the presence of specific text on a web page. This guide will walk you through the most effective methods to achieve this.

Generic Solution

For a straightforward approach, you can use the page_source attribute of the Selenium WebDriver. This method checks if the desired text is present in the HTML source of the web page.

if text in driver.page_source:
    # text exists in page

This simple condition will verify if the text exists in the page source and can be a quick way to check for text presence.

Using Unittest

If you are using the unittest framework, you can incorporate the following assertion:

from unittest import TestCase

class TestPageText(TestCase):
    def test_text_presence(self):
        self.assertTrue(text in driver.page_source)

This will assert that the text is present in the page source, providing a clear pass/fail result in your test suite.

Using Pytest

For those using pytest, the assertion is quite similar:

def test_text_presence():
    assert text in driver.page_source

This approach integrates seamlessly with pytest, ensuring that your tests are simple and readable.

Using Regular Expressions

Another method involves using regular expressions to search for the text within the page source. This can be particularly useful if you need more flexibility in matching patterns:

import re

src = driver.page_source
text_found ='text_to_search', src)
assert text_found is not None

This method provides a robust way to check for text presence, allowing for complex search patterns.

Waiting for Elements

In some cases, the text you are looking for might be part of an element that is dynamically loaded. You can use Selenium’s explicit wait functionality to wait for a specific condition:

from selenium import webdriver
from import By
from import WebDriverWait
from import expected_conditions as EC

browser = webdriver.Firefox()
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, 'some link text')))

This method waits for up to 10 seconds for the element with the specified link text to become clickable, ensuring that the text is present and interactable.

Enhancing Your Test Automation with Repeato

While these methods provide effective ways to check for text presence using Selenium, managing and maintaining tests can become complex as your application grows. This is where Repeato, a no-code test automation tool for iOS and Android, can help.

With Repeato, you can create, run, and maintain automated tests for your apps quickly and efficiently. Repeato’s intuitive test recorder and computer vision-based approach make it particularly fast to edit and run tests. Additionally, Repeato supports testing websites inside an Android emulator or device, with explicit web testing support coming soon.

For more information on how Repeato can streamline your test automation process, visit our Getting Started guide.

Like this article? there’s more where that came from!