Test Overview
This test opens a web page and finds the first <h1> tag element. It verifies that the text inside the <h1> tag matches the expected heading.
This test opens a web page and finds the first <h1> tag element. It verifies that the text inside the <h1> tag matches the expected heading.
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options import unittest class TestFindElementByTagName(unittest.TestCase): def setUp(self): options = Options() options.add_argument('--headless') service = Service() self.driver = webdriver.Chrome(service=service, options=options) def test_find_h1_by_tag_name(self): self.driver.get('https://example.com') element = self.driver.find_element(By.TAG_NAME, 'h1') self.assertEqual(element.text, 'Example Domain') def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main()
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Test framework initializes and prepares the test environment | — | PASS |
| 2 | Browser opens in headless mode | Chrome browser instance is launched without UI | — | PASS |
| 3 | Navigates to 'https://example.com' | Browser loads the Example Domain page with a visible <h1> element | — | PASS |
| 4 | Finds element by tag name 'h1' using driver.find_element(By.TAG_NAME, 'h1') | The first <h1> element on the page is located | — | PASS |
| 5 | Checks that the text of the <h1> element equals 'Example Domain' | The element text is 'Example Domain' | assertEqual(element.text, 'Example Domain') | PASS |
| 6 | Browser closes and test ends | Browser instance is terminated cleanly | — | PASS |
find_element(By.TAG_NAME, 'button') do in Selenium Python?find_element returns the first matching element found.By.TAG_NAME with 'button' searches for the first <button> tag element.p elements using Selenium Python?find_elements (plural).By.TAG_NAME with the tag string 'p'.<div> <button>Click</button> <button>Submit</button> </div>
driver.find_element(By.TAG_NAME, 'button').text return?.text attribute returns the visible text inside that element, which is "Click".element = driver.find_elements(By.TAG_NAME, 'img')
element.click(). What is the problem?click() on a list causes an error because lists don't have click method.button on a page. Which code correctly finds and clicks it using tag name?find_elements(By.TAG_NAME, 'button') to get a list of all button elements.click() on it.Pass the test and this pattern moves unknown