Test Overview
This test opens a web page, finds a heading element, retrieves its text, and verifies that the text matches the expected value.
This test opens a web page, finds a heading element, retrieves its text, and verifies that the text matches the expected value.
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()
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Browser window is open and ready | — | PASS |
| 2 | Navigates to 'https://example.com' | Page loads with heading 'Example Domain' | — | PASS |
| 3 | Finds element with tag name 'h1' | Element <h1> with text 'Example Domain' is located | — | PASS |
| 4 | Retrieves text from the heading element | Text value is 'Example Domain' | — | PASS |
| 5 | Checks if the text equals 'Example Domain' | Text matches expected string | assertEqual(text, 'Example Domain') | PASS |
| 6 | Closes the browser | Browser window is closed | — | PASS |
.text property do in Selenium when used on a web element?.text.text property in Selenium is used to get the visible text content from a web element..text..text gets visible text [OK]element in Selenium Python?.text without parentheses.getText() and get_text() are not valid in Selenium Python. text() with parentheses is incorrect because text is a property, not a method.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)
.text..text returnsbutton.text returns this string.element_text = driver.find_element(By.CLASS_NAME, "title").text()
.text is a property and should not have parentheses. Using .text() causes a TypeError..text property without parentheses to get visible text.getText() and get_text are invalid in Selenium Python; text() with parentheses is wrong.Pass the test and this pattern moves unknown