Browser-specific workarounds in Selenium Python - Build an Automation Script
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.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options import platform class LoginPage: def __init__(self, driver): self.driver = driver self.username_input = (By.ID, 'username') self.password_input = (By.ID, 'password') self.login_button = (By.ID, 'loginBtn') def enter_username(self, username): WebDriverWait(self.driver, 10).until( EC.visibility_of_element_located(self.username_input) ).clear() self.driver.find_element(*self.username_input).send_keys(username) def enter_password(self, password): WebDriverWait(self.driver, 10).until( EC.visibility_of_element_located(self.password_input) ).clear() self.driver.find_element(*self.password_input).send_keys(password) def click_login(self): WebDriverWait(self.driver, 10).until( EC.element_to_be_clickable(self.login_button) ).click() def test_login_with_chrome_workaround(): options = Options() options.add_argument('--headless=new') # Use headless mode for faster test service = Service() driver = webdriver.Chrome(service=service, options=options) try: driver.get('https://example.com/login') login_page = LoginPage(driver) login_page.enter_username('testuser') login_page.enter_password('Test@1234') # Browser-specific workaround for Chrome if 'chrome' in driver.capabilities.get('browserName', '').lower(): # Remove any overlay that blocks clicks using JS driver.execute_script(""" const overlay = document.querySelector('.overlay'); if (overlay) { overlay.style.display = 'none'; } """) login_page.click_login() # Wait for URL to change to dashboard WebDriverWait(driver, 10).until( EC.url_to_be('https://example.com/dashboard') ) # Assert URL assert driver.current_url == 'https://example.com/dashboard', f"Expected URL to be dashboard but got {driver.current_url}" # Assert page title contains 'Dashboard' WebDriverWait(driver, 10).until( EC.title_contains('Dashboard') ) assert 'Dashboard' in driver.title, f"Expected title to contain 'Dashboard' but got {driver.title}" finally: driver.quit()
This test script uses Selenium with Python to automate the login process on a website.
We define a LoginPage class to keep selectors and actions organized, following the Page Object Model.
We open the login page, enter username and password, then check if the browser is Chrome by inspecting driver.capabilities. If it is Chrome, we run JavaScript to hide any overlay that might block clicks. This is the browser-specific workaround.
We then click the login button and wait explicitly for the URL to change to the dashboard page and for the page title to contain 'Dashboard'.
Assertions verify the URL and title to confirm successful login.
Explicit waits ensure the test waits only as long as needed, avoiding flaky tests.
Finally, the browser is closed in a finally block to clean up.
Now add data-driven testing with 3 different username and password combinations to verify login success or failure.