Verify login button is clickable using implicit wait
Preconditions (2)
✅ Expected Result: The login button is found and clicked successfully within the implicit wait time, and the dashboard page loads confirming successful login
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.
Now add data-driven testing with 3 different username and password combinations
implicit waits in Selenium WebDriver?driver.implicitly_wait(seconds).from selenium import webdriver
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome()
driver.implicitly_wait(5)
start = time.time()
try:
element = driver.find_element(By.ID, 'nonexistent')
except:
pass
end = time.time()
print(round(end - start))driver = webdriver.Chrome() driver.implicitly_wait = 10 button = driver.find_element(By.ID, 'submit')
driver.implicitly_wait(10), not by assigning to driver.implicitly_wait.Pass the test and this pattern moves unknown