Simple alert acceptance 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.common.exceptions import TimeoutException # Initialize the WebDriver (e.g., Chrome) driver = webdriver.Chrome() try: driver.get('https://example.com/alert') # Replace with actual URL # Locate the button that triggers the alert alert_button = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.ID, 'alertButton')) ) # Click the button to open the alert alert_button.click() # Wait for the alert to be present WebDriverWait(driver, 10).until(EC.alert_is_present()) # Switch to the alert alert = driver.switch_to.alert # Assert alert text is not empty (optional check) assert alert.text != '', 'Alert text should not be empty' # Accept the alert alert.accept() # Verify alert is no longer present try: WebDriverWait(driver, 3).until(EC.alert_is_present()) alert_present = True except TimeoutException: alert_present = False assert not alert_present, 'Alert should be closed after acceptance' finally: driver.quit()
This script starts by opening the browser and navigating to the page with the alert button.
It waits explicitly for the alert button to be clickable, then clicks it to trigger the alert popup.
Next, it waits for the alert to appear using an explicit wait, then switches to the alert using Selenium's Alert API.
It asserts that the alert text is not empty to confirm the alert is present.
Then it accepts the alert to close it.
Finally, it verifies that the alert is no longer present by waiting briefly and catching a timeout exception if no alert appears.
The driver quits at the end to close the browser.
Now add data-driven testing with 3 different alert buttons that trigger alerts with different messages
Practice
driver.switch_to.alert.accept() command do in Selenium Python?Solution
Step 1: Understand alert handling in Selenium
Thedriver.switch_to.alertaccesses the alert popup currently shown by the browser.Step 2: Accepting the alert
Theaccept()method clicks the OK button on the alert, closing it.Final Answer:
It clicks the OK button on a simple alert popup. -> Option CQuick Check:
accept() = clicks OK [OK]
- Confusing accept() with dismiss() which clicks Cancel
- Trying to send keys with accept()
- Assuming accept() closes the browser
Solution
Step 1: Recall correct Selenium Python syntax
The correct way to switch to alert isdriver.switch_to.alert.Step 2: Confirm method chaining
Callingaccept()on the alert object accepts the alert.Final Answer:
driver.switch_to.alert.accept() -> Option BQuick Check:
Correct syntax = driver.switch_to.alert.accept() [OK]
- Using Java-style syntax like switchTo()
- Missing the switch_to part
- Using underscores incorrectly in method names
driver.switch_to.alert.accept()
print("Alert accepted")Solution
Step 1: Accept the alert
Theaccept()method clicks OK on the alert, closing it.Step 2: Print confirmation
After alert is accepted, the print statement runs and outputs the message.Final Answer:
The alert is accepted and 'Alert accepted' is printed. -> Option DQuick Check:
alert.accept() then print = alert accepted message [OK]
- Thinking alert is ignored without accept()
- Expecting print before alert is accepted
- Assuming exception if alert is present
alert = driver.switch_to.alert alert.accept
Solution
Step 1: Check method call syntax
Theacceptmethod must be called with parenthesesaccept()to execute.Step 2: Identify missing parentheses
Without parentheses,alert.acceptis just a reference, not a call, so alert is not accepted.Final Answer:
Missing parentheses after accept method call. -> Option AQuick Check:
Method calls need () to run [OK]
- Forgetting parentheses on method calls
- Using wrong variable names
- Calling accept() on driver instead of alert
Solution
Step 1: Understand alert presence check
Selenium throws an exception if alert is not present, so we must catch it.Step 2: Use try-except to accept alert safely
Wrappingaccept()in try-except avoids errors if alert is missing.Final Answer:
try: driver.switch_to.alert.accept() except: pass -> Option AQuick Check:
Use try-except to handle optional alerts [OK]
- Checking alert presence with if directly
- Calling accept() without error handling
- Using undefined alert variable
Ready to prove it?
Pass the test and this pattern moves unknown
