0
0
Selenium Pythontesting~15 mins

Double click in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Verify double click action triggers alert
Preconditions (2)
Step 1: Open the test page URL in the browser
Step 2: Locate the button with id 'double-click-btn'
Step 3: Perform a double click on the button
Step 4: Switch to the alert popup
✅ Expected Result: An alert popup appears with text 'Double click successful!'
Automation Requirements - Selenium with Python
Assertions Needed:
Verify alert is present after double click
Verify alert text equals 'Double click successful!'
Best Practices:
Use explicit waits to wait for elements and alerts
Use ActionChains for double click action
Handle alert properly by switching to it
Use clear and maintainable locators (By.ID)
Automated Solution
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# Setup WebDriver (assumes chromedriver is in PATH)
driver = webdriver.Chrome()

try:
    # Open the test page
    driver.get('https://example.com/double-click-test')

    # Wait for the button to be clickable
    wait = WebDriverWait(driver, 10)
    button = wait.until(EC.element_to_be_clickable((By.ID, 'double-click-btn')))

    # Perform double click on the button
    actions = ActionChains(driver)
    actions.double_click(button).perform()

    # Wait for alert to be present
    alert = wait.until(EC.alert_is_present())

    # Verify alert text
    alert_text = alert.text
    assert alert_text == 'Double click successful!', f"Expected alert text to be 'Double click successful!' but got '{alert_text}'"

    # Accept the alert to close it
    alert.accept()

finally:
    driver.quit()

This script opens the test page URL where the double click button is located. It waits explicitly for the button to be clickable to avoid timing issues. Then it uses Selenium's ActionChains to perform a double click on the button. After the double click, it waits for the alert popup to appear using an explicit wait. Once the alert is present, it verifies the alert text matches the expected message. Finally, it accepts the alert to close it and quits the browser. This approach uses best practices like explicit waits, proper locator usage, and clean action handling.

Common Mistakes - 4 Pitfalls
Using time.sleep() instead of explicit waits
Using incorrect locator like XPath with absolute path
Not switching to alert before accessing its text
Using click() twice instead of double_click()
Bonus Challenge

Now add data-driven testing with 3 different buttons that trigger different alerts on double click

Show Hint