Bird
Raised Fist0
Selenium Javatesting~20 mins

Alert accept and dismiss in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Alert Mastery
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 Java code snippet that handles a JavaScript alert. What will be the printed output after the alert is accepted?
Selenium Java
WebDriver driver = new ChromeDriver();
driver.get("https://example.com/alert");
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
alert.accept();
System.out.println(alertText);
APrints null
BNo output is printed because accept() closes the alert silently
CThrows NoAlertPresentException
DThe text inside the alert box is printed
Attempts:
2 left
💡 Hint
Remember that getText() reads the alert message before accepting it.
❓ assertion
intermediate
1:30remaining
Which assertion correctly verifies alert dismissal?
You want to verify that after dismissing an alert, a label on the page shows "Cancelled". Which assertion is correct?
Selenium Java
Alert alert = driver.switchTo().alert();
alert.dismiss();
String labelText = driver.findElement(By.id("resultLabel")).getText();
AassertEquals("Cancelled", labelText);
BassertTrue(labelText.contains("Cancelled"));
CassertFalse(labelText.equals("Cancelled"));
DassertNull(labelText);
Attempts:
2 left
💡 Hint
Check exact match of the label text after dismissing the alert.
🔧 Debug
advanced
2:00remaining
Why does this alert accept code throw NoAlertPresentException?
Analyze the code below. Why does it throw NoAlertPresentException at runtime?
Selenium Java
driver.get("https://example.com/noalert");
Alert alert = driver.switchTo().alert();
alert.accept();
ANo alert is present on the page when switchTo().alert() is called
BThe alert is already accepted before calling accept()
CThe alert text is empty
DThe driver instance is null
Attempts:
2 left
💡 Hint
Check if the page actually shows an alert before switching.
❓ framework
advanced
2:00remaining
Which Selenium wait strategy best handles alerts appearing asynchronously?
You want to wait for an alert to appear before accepting it. Which Selenium wait approach is best?
AUse implicit wait set to 10 seconds
BUse Thread.sleep(5000) before switching to alert
CUse WebDriverWait with ExpectedConditions.alertIsPresent()
DUse FluentWait ignoring NoAlertPresentException without condition
Attempts:
2 left
💡 Hint
Look for a wait condition specifically for alerts.
🧠 Conceptual
expert
2:30remaining
What happens if you call dismiss() on a prompt alert without sending input?
Consider a JavaScript prompt alert that asks for user input. What is the expected behavior if you call dismiss() without sending any text?
AThe prompt throws an exception because input is required
BThe prompt is closed and the input value is null or empty
CThe prompt accepts with default text automatically
DThe prompt remains open until input is sent
Attempts:
2 left
💡 Hint
Dismiss means canceling the prompt without input.

Practice

(1/5)
1. What does the accept() method do when called on a JavaScript alert in Selenium?
easy
A. Clicks the OK button on the alert
B. Clicks the Cancel button on the alert
C. Closes the browser window
D. Throws an exception without interacting

Solution

  1. Step 1: Understand alert interaction methods

    The accept() method is used to click the OK button on an alert popup.
  2. Step 2: Differentiate from dismiss()

    The dismiss() method clicks Cancel, so accept() must click OK.
  3. Final Answer:

    Clicks the OK button on the alert -> Option A
  4. Quick Check:

    accept() = OK click [OK]
Hint: accept() clicks OK, dismiss() clicks Cancel [OK]
Common Mistakes:
  • Confusing accept() with dismiss()
  • Thinking accept() closes the browser
  • Assuming accept() throws an exception
2. Which of the following is the correct syntax to switch to an alert and dismiss it in Selenium Java?
easy
A. driver.switchTo().alert().dismiss();
B. driver.switchTo().alert().cancel();
C. driver.switchTo().alert().close();
D. driver.switchTo().alert().accept();

Solution

  1. Step 1: Identify the method to dismiss alert

    The method to click Cancel on an alert is dismiss().
  2. Step 2: Confirm correct syntax for switching and dismissing

    The correct chain is driver.switchTo().alert().dismiss(); to dismiss the alert.
  3. Final Answer:

    driver.switchTo().alert().dismiss(); -> Option A
  4. Quick Check:

    dismiss() = Cancel click [OK]
Hint: Use dismiss() to cancel alerts, accept() to confirm [OK]
Common Mistakes:
  • Using accept() instead of dismiss() to cancel
  • Using non-existent methods like close() or cancel()
  • Missing switchTo().alert() before calling dismiss()
3. Given the following code snippet, what will be printed if the alert is accepted?
driver.switchTo().alert().accept();
System.out.println("Alert handled successfully");
medium
A. UnhandledAlertException
B. Alert handled successfully
C. No output
D. Alert not found error

Solution

  1. Step 1: Understand alert acceptance effect

    Calling accept() closes the alert, allowing the next line to execute.
  2. Step 2: Check output after alert handling

    Since alert is accepted, System.out.println runs and prints the message.
  3. Final Answer:

    Alert handled successfully -> Option B
  4. Quick Check:

    accept() closes alert, next line prints [OK]
Hint: accept() lets code continue, so print runs [OK]
Common Mistakes:
  • Expecting exception after accept()
  • Thinking alert blocks print statement
  • Confusing accept() with dismiss() effect
4. Identify the error in this Selenium Java code snippet that tries to dismiss an alert:
Alert alert = driver.switchTo().alert();
alert.accept();
alert.dismiss();
medium
A. alert.dismiss() should be alert.cancel()
B. Calling accept() before dismiss() causes an exception
C. Missing driver.switchTo() before alert()
D. Cannot call dismiss() after accept() on the same alert instance

Solution

  1. Step 1: Understand alert lifecycle after accept()

    Once accept() is called, the alert is closed and no longer available.
  2. Step 2: Calling dismiss() after accept() is invalid

    Trying to dismiss the same alert after accepting it causes an error because alert is gone.
  3. Final Answer:

    Cannot call dismiss() after accept() on the same alert instance -> Option D
  4. Quick Check:

    accept() closes alert, no dismiss() after [OK]
Hint: Alert closes after accept(), can't dismiss same alert [OK]
Common Mistakes:
  • Calling dismiss() after accept() on same alert
  • Assuming alert stays open after accept()
  • Using non-existent cancel() method
5. You want to write a Selenium Java test that clicks Cancel on a confirmation alert only if the alert text contains the word "Delete". Which code snippet correctly implements this?
hard
A. Alert alert = driver.switchTo().alert(); if (alert.getText() == "Delete") { alert.accept(); } else { alert.dismiss(); }
B. Alert alert = driver.switchTo().alert(); alert.dismiss(); if (alert.getText().contains("Delete")) { alert.accept(); }
C. Alert alert = driver.switchTo().alert(); if (alert.getText().contains("Delete")) { alert.dismiss(); } else { alert.accept(); }
D. Alert alert = driver.switchTo().alert(); alert.accept(); alert.dismiss();

Solution

  1. Step 1: Check alert text for "Delete" substring

    Use alert.getText().contains("Delete") to detect if alert text includes "Delete".
  2. Step 2: Dismiss alert if condition true, else accept

    If text contains "Delete", call dismiss() to click Cancel; otherwise, call accept().
  3. Final Answer:

    Alert alert = driver.switchTo().alert(); if (alert.getText().contains("Delete")) { alert.dismiss(); } else { alert.accept(); } -> Option C
  4. Quick Check:

    Use contains() and dismiss() for conditional cancel [OK]
Hint: Use getText().contains() before dismiss() or accept() [OK]
Common Mistakes:
  • Using == instead of contains() for string check
  • Calling dismiss() before checking alert text
  • Calling accept() and dismiss() both without condition
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