Verify login button is clickable by finding element using ID
Preconditions (1)
✅ Expected Result: The login button is found by ID and clicked successfully without errors
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 # Initialize the Chrome driver with webdriver.Chrome() as driver: driver.get('https://example.com/login') wait = WebDriverWait(driver, 10) # Wait until the login button is present and clickable login_button = wait.until(EC.element_to_be_clickable((By.ID, 'loginBtn'))) # Assert the login button is displayed assert login_button.is_displayed(), 'Login button is not visible' # 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', 'Did not navigate to dashboard after login'
This script uses Selenium WebDriver with Python to automate the manual test case.
First, it opens the login page URL.
Then it uses an explicit wait to find the login button by its ID 'loginBtn' and waits until it is clickable.
It asserts the button is visible before clicking it.
After clicking, it waits for the URL to change to the dashboard URL and asserts the navigation succeeded.
Using explicit waits ensures the test does not fail due to timing issues.
Using By.ID locator is the most direct and reliable way to find the element by its unique ID.
Now add data-driven testing with 3 different login button IDs to verify each is clickable
find_element(By.ID, "id") with the By class.By.ID for ID locator [OK]By.ID in Selenium Python?By class is located in selenium.webdriver.common.by module.By. import selenium.webdriver.By as By uses invalid syntax. from selenium.webdriver.common.by import ID tries to import ID which doesn't exist. from selenium.webdriver import By imports By from wrong module.By from common.by [OK]from selenium import webdriver from selenium.webdriver.common.by import By driver = webdriver.Chrome() element = driver.find_element(By.ID, "submit-btn") print(element.tag_name)
find_element returns the WebElement object.element.tag_name returnstag_name attribute returns the HTML tag name of the found element, e.g., 'button' or 'input'.from selenium import webdriver from selenium.webdriver.common.by import By driver = webdriver.Chrome() element = driver.find_element(By.id, "username")
By class attributes are uppercase, so it should be By.ID not By.id.webdriver.Chrome() are present. The method find_element with By.ID is correct syntax.NoSuchElementException. To avoid test failure, catch this exception.Pass the test and this pattern moves unknown