Verify login button is clickable using XPath locator
Preconditions (3)
✅ Expected Result: The login button is found by XPath and clicked successfully, and the browser navigates to the dashboard 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 # Setup WebDriver with webdriver.Chrome() as driver: driver.get('https://example.com/login') # Wait until the login button is clickable wait = WebDriverWait(driver, 10) login_button = wait.until(EC.element_to_be_clickable((By.XPATH, '//button[@id="loginBtn"]'))) # Assert the login button is found assert login_button is not None, "Login button not found by XPath" # Click the login button login_button.click() # Wait until URL changes to dashboard wait.until(EC.url_to_be('https://example.com/dashboard')) # Assert the URL is correct assert driver.current_url == 'https://example.com/dashboard', f"Expected URL to be 'https://example.com/dashboard' but got {driver.current_url}"
This script uses Selenium WebDriver with Python to automate the manual test case.
First, it opens the Chrome browser and navigates to the login page URL.
It uses an explicit wait to wait up to 10 seconds for the login button to become clickable, locating it by XPath //button[@id="loginBtn"].
Then it asserts the button is found (not None), clicks it, and waits until the URL changes to the dashboard URL.
Finally, it asserts the current URL matches the expected dashboard URL.
The with statement ensures the browser closes automatically after the test.
Now add data-driven testing with 3 different XPath locators for login buttons on different pages
driver.find_element(By.XPATH, "//button[@id='submit']")find_element returns the first matching element found by the locator.//button[@id='submit'] selects button elements with attribute id='submit'.driver.find_element(By.XPATH, xpath_string).<input name='search'> element?element = driver.find_element(By.XPATH, "//input[@name='search']") print(element.tag_name)
input tag, so element.tag_name returns "input".element = driver.find_element(By.XPATH, "//div[@class='content']") print(element.text)
<div class='content'> elements.div.content exist, only the first is found and printed, others ignored.driver.find_element(By.XPATH, xpath)?text()='Submit' matches elements whose text content is exactly 'Submit'.Pass the test and this pattern moves unknown