0
0
Selenium Pythontesting~15 mins

Implicit waits in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Verify login button is clickable using implicit wait
Preconditions (2)
Step 1: Open the browser and navigate to 'https://example.com/login'
Step 2: Set implicit wait to 10 seconds
Step 3: Locate the username input field and enter 'testuser'
Step 4: Locate the password input field and enter 'Test@1234'
Step 5: Locate the login button and click it
Step 6: Wait for the dashboard page to load
✅ Expected Result: The login button is found and clicked successfully within the implicit wait time, and the dashboard page loads confirming successful login
Automation Requirements - Selenium with Python
Assertions Needed:
Verify the current URL contains '/dashboard' after login
Verify the login button is clickable before clicking
Best Practices:
Use implicit wait only once after driver initialization
Use By.ID or By.NAME locators for better reliability
Avoid mixing implicit and explicit waits
Close the browser after test execution
Automated Solution
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
import time

def test_login_with_implicit_wait():
    driver = webdriver.Chrome()
    try:
        # Set implicit wait once
        driver.implicitly_wait(10)

        # Open login page
        driver.get('https://example.com/login')

        # Enter username
        username_field = driver.find_element(By.ID, 'username')
        username_field.send_keys('testuser')

        # Enter password
        password_field = driver.find_element(By.ID, 'password')
        password_field.send_keys('Test@1234')

        # Find and click login button
        login_button = driver.find_element(By.ID, 'loginBtn')
        login_button.click()

        # After click, verify URL contains '/dashboard'
        # Wait a short time to allow page load
        time.sleep(2)
        assert '/dashboard' in driver.current_url, f"Expected '/dashboard' in URL but got {driver.current_url}"

    finally:
        driver.quit()

This script starts by creating a Chrome WebDriver instance.

It sets an implicit wait of 10 seconds once, so Selenium will wait up to 10 seconds when searching for elements.

Then it opens the login page URL.

It finds the username and password fields by their IDs and enters the test credentials.

It locates the login button by ID and clicks it.

After clicking, it waits 2 seconds to allow the dashboard page to load.

Finally, it asserts that the current URL contains '/dashboard' to confirm successful login.

The driver is closed in the finally block to ensure cleanup.

Common Mistakes - 4 Pitfalls
Setting implicit wait multiple times in the test
Mixing implicit waits with explicit waits
Using hardcoded sleep instead of implicit wait
Using unreliable locators like XPath with absolute paths
Bonus Challenge

Now add data-driven testing with 3 different username and password combinations

Show Hint