Bird
Raised Fist0
Selenium Pythontesting~20 mins

Simple alert acceptance in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Alert Acceptance Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output after accepting the alert?
Consider the following Selenium Python code that triggers a simple alert and accepts it. What will be printed after the alert is accepted?
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.alert import Alert

# Setup driver (assume driver is correctly initialized)
driver = webdriver.Chrome()
driver.get('data:text/html,<button onclick="alert(\'Hello!\')" id="btn">Click</button>')

button = driver.find_element(By.ID, 'btn')
button.click()

alert = Alert(driver)
alert.accept()

print('Alert accepted')
driver.quit()
ATimeoutException
BUnhandledAlertException
CNoSuchElementException
DAlert accepted
Attempts:
2 left
💡 Hint
Think about what happens after alert.accept() is called and what the print statement does.
❓ assertion
intermediate
1:30remaining
Which assertion correctly verifies alert text?
You want to verify that a simple alert shows the text 'Warning!'. Which assertion is correct after switching to the alert?
Selenium Python
alert = driver.switch_to.alert
alert_text = alert.text
Aassert alert_text == 'Warning!'
Bassert alert.text = 'Warning!'
Cassert alert_text.equals('Warning!')
Dassert alert_text != 'Warning!'
Attempts:
2 left
💡 Hint
Remember how to compare strings in Python assertions.
❓ locator
advanced
1:30remaining
Choose the best locator to find a button triggering a simple alert
You have a button that triggers a simple alert when clicked. The button has the text 'Show Alert' but no id or class attributes. Which locator is best to find this button?
Adriver.find_element(By.CSS_SELECTOR, "button#show-alert")
Bdriver.find_element(By.ID, "show-alert")
Cdriver.find_element(By.XPATH, "//button[text()='Show Alert']")
Ddriver.find_element(By.CLASS_NAME, "alert-button")
Attempts:
2 left
💡 Hint
The button has no id or class, but has visible text.
🔧 Debug
advanced
2:00remaining
Why does alert.accept() raise UnexpectedAlertPresentException?
You run this code: button.click() alert.accept() But get UnexpectedAlertPresentException on alert.accept(). What is the likely cause?
AThe alert was not switched to before calling accept()
BThe alert text is empty
CThe driver is not initialized
DThe button was not clicked
Attempts:
2 left
💡 Hint
You must switch to the alert before accepting it.
❓ framework
expert
3:00remaining
Which pytest fixture correctly handles alert acceptance for multiple tests?
You want to create a pytest fixture that clicks a button triggering a simple alert and accepts it before each test. Which fixture code is correct?
Selenium Python
import pytest
from selenium.webdriver.common.by import By
from selenium.webdriver.common.alert import Alert

@pytest.fixture
def accept_alert_before_test(driver):
    button = driver.find_element(By.ID, 'alert-btn')
    button.click()
    alert = Alert(driver)
    alert.accept()
    yield

# Tests use this fixture
A
@pytest.fixture
def accept_alert_before_test(driver):
    button = driver.find_element(By.ID, 'alert-btn')
    button.click()
    alert = Alert(driver)
    alert.accept()
    yield
B
@pytest.fixture
def accept_alert_before_test(driver):
    button = driver.find_element(By.ID, 'alert-btn')
    button.click()
    alert = driver.switch_to.alert
    alert.accept()
    yield
C
@pytest.fixture
def accept_alert_before_test(driver):
    button = driver.find_element(By.ID, 'alert-btn')
    button.click()
    alert = driver.switch_to.alert
    alert.dismiss()
    yield
D
@pytest.fixture
def accept_alert_before_test(driver):
    button = driver.find_element(By.ID, 'alert-btn')
    button.click()
    alert = Alert(driver)
    alert.dismiss()
    yield
Attempts:
2 left
💡 Hint
Use driver.switch_to.alert to get the alert object before accepting.

Practice

(1/5)
1. What does the driver.switch_to.alert.accept() command do in Selenium Python?
easy
A. It closes the browser window.
B. It refreshes the current page.
C. It clicks the OK button on a simple alert popup.
D. It sends text input to the alert popup.

Solution

  1. Step 1: Understand alert handling in Selenium

    The driver.switch_to.alert accesses the alert popup currently shown by the browser.
  2. Step 2: Accepting the alert

    The accept() method clicks the OK button on the alert, closing it.
  3. Final Answer:

    It clicks the OK button on a simple alert popup. -> Option C
  4. Quick Check:

    accept() = clicks OK [OK]
Hint: accept() always clicks OK on alert popups [OK]
Common Mistakes:
  • Confusing accept() with dismiss() which clicks Cancel
  • Trying to send keys with accept()
  • Assuming accept() closes the browser
2. Which of the following is the correct syntax to accept a simple alert in Selenium Python?
easy
A. driver.alert.accept()
B. driver.switch_to.alert.accept()
C. driver.switch_to_alert.accept()
D. driver.switchTo().alert().accept()

Solution

  1. Step 1: Recall correct Selenium Python syntax

    The correct way to switch to alert is driver.switch_to.alert.
  2. Step 2: Confirm method chaining

    Calling accept() on the alert object accepts the alert.
  3. Final Answer:

    driver.switch_to.alert.accept() -> Option B
  4. Quick Check:

    Correct syntax = driver.switch_to.alert.accept() [OK]
Hint: Use driver.switch_to.alert.accept() to accept alerts [OK]
Common Mistakes:
  • Using Java-style syntax like switchTo()
  • Missing the switch_to part
  • Using underscores incorrectly in method names
3. What will happen when the following code runs if a simple alert is present?
driver.switch_to.alert.accept()
print("Alert accepted")
medium
A. The alert is accepted but nothing is printed.
B. The alert is ignored and 'Alert accepted' is printed.
C. An exception is thrown because alert is not handled.
D. The alert is accepted and 'Alert accepted' is printed.

Solution

  1. Step 1: Accept the alert

    The accept() method clicks OK on the alert, closing it.
  2. Step 2: Print confirmation

    After alert is accepted, the print statement runs and outputs the message.
  3. Final Answer:

    The alert is accepted and 'Alert accepted' is printed. -> Option D
  4. Quick Check:

    alert.accept() then print = alert accepted message [OK]
Hint: accept() closes alert, next lines run normally [OK]
Common Mistakes:
  • Thinking alert is ignored without accept()
  • Expecting print before alert is accepted
  • Assuming exception if alert is present
4. Identify the error in this code snippet for accepting an alert:
alert = driver.switch_to.alert
alert.accept
medium
A. Missing parentheses after accept method call.
B. Incorrect alert switching syntax.
C. alert variable name is reserved.
D. accept() should be called on driver, not alert.

Solution

  1. Step 1: Check method call syntax

    The accept method must be called with parentheses accept() to execute.
  2. Step 2: Identify missing parentheses

    Without parentheses, alert.accept is just a reference, not a call, so alert is not accepted.
  3. Final Answer:

    Missing parentheses after accept method call. -> Option A
  4. Quick Check:

    Method calls need () to run [OK]
Hint: Always add () to call methods like accept() [OK]
Common Mistakes:
  • Forgetting parentheses on method calls
  • Using wrong variable names
  • Calling accept() on driver instead of alert
5. You want to accept a simple alert only if it is present, without causing an error if no alert shows. Which code snippet correctly does this?
hard
A. try: driver.switch_to.alert.accept() except: pass
B. if driver.switch_to.alert: driver.switch_to.alert.accept()
C. driver.switch_to.alert.accept() if alert else None
D. driver.switch_to.alert.accept()

Solution

  1. Step 1: Understand alert presence check

    Selenium throws an exception if alert is not present, so we must catch it.
  2. Step 2: Use try-except to accept alert safely

    Wrapping accept() in try-except avoids errors if alert is missing.
  3. Final Answer:

    try: driver.switch_to.alert.accept() except: pass -> Option A
  4. Quick Check:

    Use try-except to handle optional alerts [OK]
Hint: Wrap alert accept in try-except to avoid errors [OK]
Common Mistakes:
  • Checking alert presence with if directly
  • Calling accept() without error handling
  • Using undefined alert variable
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard