Verify presence of header element by tag name
Preconditions (2)
✅ Expected Result: The <h1> element is found and visible on the page
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import TimeoutException # Initialize the Chrome WebDriver with webdriver.Chrome() as driver: driver.get('https://example.com') try: # Wait up to 10 seconds for the <h1> element to be present h1_element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.TAG_NAME, 'h1')) ) # Assert the element is displayed assert h1_element.is_displayed(), 'The <h1> element is not visible on the page.' print('Test Passed: <h1> element found and visible.') except TimeoutException: print('Test Failed: <h1> element not found within 10 seconds.') assert False, 'The <h1> element was not found on the page.'
This script uses Selenium WebDriver with Python to automate the test case.
First, it opens the browser and navigates to the specified URL.
Then, it waits explicitly up to 10 seconds for the <h1> tag element to appear on the page using WebDriverWait and presence_of_element_located with By.TAG_NAME.
Once found, it asserts that the element is visible using is_displayed(). If the element is not found within the timeout, it catches the TimeoutException and fails the test.
The with statement ensures the browser closes automatically after the test.
Now add data-driven testing to verify presence of multiple tag names: 'h1', 'p', and 'footer' on the page.
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