0
0
Selenium Pythontesting~15 mins

Find element by name in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Verify login button is clickable by finding element using name attribute
Preconditions (2)
Step 1: Open the browser and navigate to https://example.com/login
Step 2: Locate the login button using its name attribute 'loginButton'
Step 3: Click the login button
✅ Expected Result: The login button is found by its name attribute and clicked successfully without errors
Automation Requirements - Selenium with Python
Assertions Needed:
Verify the login button is present before clicking
Verify the login button is clickable
Verify no exceptions occur during element location and click
Best Practices:
Use explicit waits to wait for the element to be present and clickable
Use By.NAME locator strategy
Handle exceptions gracefully
Close the browser after test execution
Automated Solution
Selenium Python
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/login')

    try:
        # Wait up to 10 seconds for the login button to be present and clickable
        wait = WebDriverWait(driver, 10)
        login_button = wait.until(EC.element_to_be_clickable((By.NAME, 'loginButton')))

        # Assert the button is displayed
        assert login_button.is_displayed(), 'Login button is not visible'

        # Click the login button
        login_button.click()

        # If needed, add further assertions here

    except TimeoutException:
        assert False, 'Login button was not found or not clickable within 10 seconds'

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 be clickable, locating it by its name attribute 'loginButton'.

It asserts that the button is visible before clicking it to ensure the element is interactable.

If the button is not found or not clickable within the wait time, the test fails with a clear message.

The with statement ensures the browser closes automatically after the test finishes.

Common Mistakes - 3 Pitfalls
{'mistake': "Using driver.find_element(By.NAME, 'loginButton') without waits", 'why_bad': 'The element might not be loaded yet, causing NoSuchElementException or ElementNotInteractableException.', 'correct_approach': 'Use explicit waits like WebDriverWait with expected_conditions to wait for the element to be present and clickable.'}
{'mistake': 'Using incorrect locator like By.ID or By.CLASS_NAME instead of By.NAME', 'why_bad': "The test will fail to find the element if the locator does not match the element's attributes.", 'correct_approach': 'Use the correct locator By.NAME with the exact name attribute value.'}
Not closing the browser after test execution
Bonus Challenge

Now add data-driven testing with 3 different name attributes for buttons to verify each is clickable

Show Hint