0
0
Selenium Pythontesting~10 mins

Getting element text in Selenium Python - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a web page, finds a heading element, retrieves its text, and verifies that the text matches the expected value.

Test Code - unittest
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
import unittest

class TestGetElementText(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://example.com')

    def test_heading_text(self):
        heading = self.driver.find_element(By.TAG_NAME, 'h1')
        text = heading.text
        self.assertEqual(text, 'Example Domain')

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensBrowser window is open and ready-PASS
2Navigates to 'https://example.com'Page loads with heading 'Example Domain'-PASS
3Finds element with tag name 'h1'Element <h1> with text 'Example Domain' is located-PASS
4Retrieves text from the heading elementText value is 'Example Domain'-PASS
5Checks if the text equals 'Example Domain'Text matches expected stringassertEqual(text, 'Example Domain')PASS
6Closes the browserBrowser window is closed-PASS
Failure Scenario
Failing Condition: The heading element is not found or text does not match 'Example Domain'
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify after finding the <h1> element?
AThat the element is visible on the page
BThat the element's text is 'Example Domain'
CThat the element is clickable
DThat the element has a specific CSS class
Key Result
Always verify the exact text content of elements to ensure the UI shows correct information, using precise locators and assertions.