An alert is a popup box that appears on the webpage to show messages or ask for user confirmation. Selenium can switch to this alert to interact with it.
Click to reveal answer
beginner
How do you accept an alert in Selenium Java?
Use driver.switchTo().alert().accept(); to click the OK button on the alert popup.
Click to reveal answer
beginner
How do you dismiss an alert in Selenium Java?
Use driver.switchTo().alert().dismiss(); to click the Cancel button or close the alert popup.
Click to reveal answer
intermediate
What happens if you try to interact with an alert without switching to it first?
Selenium throws a NoAlertPresentException because it cannot find the alert unless you switch to it first.
Click to reveal answer
intermediate
Write the code snippet to accept an alert and verify the alert text is 'Are you sure?'.
String alertText = driver.switchTo().alert().getText();
assert alertText.equals("Are you sure?");
driver.switchTo().alert().accept();
Click to reveal answer
Which Selenium method is used to accept an alert popup?
Adriver.switchTo().alert().accept()
Bdriver.switchTo().alert().dismiss()
Cdriver.acceptAlert()
Ddriver.alert().accept()
✗ Incorrect
The correct method to accept an alert is driver.switchTo().alert().accept().
What does driver.switchTo().alert().dismiss() do?
AClicks Cancel or closes the alert
BThrows an exception
CClicks OK on the alert
DSwitches to the alert without any action
✗ Incorrect
Dismiss() clicks Cancel or closes the alert popup.
Before accepting or dismissing an alert, what must you do?
ARefresh the page
BNo action needed
CClose the browser
DSwitch to the alert using driver.switchTo().alert()
✗ Incorrect
You must switch to the alert first to interact with it.
What exception is thrown if you try to accept an alert when none is present?
ATimeoutException
BNoSuchElementException
CNoAlertPresentException
DElementNotVisibleException
✗ Incorrect
NoAlertPresentException is thrown when no alert is found.
How do you get the text displayed on an alert popup?
Adriver.getAlertText()
Bdriver.switchTo().alert().getText()
Cdriver.alertText()
Ddriver.getText()
✗ Incorrect
Use driver.switchTo().alert().getText() to read alert text.
Explain the steps to handle an alert popup in Selenium Java.
Think about how you interact with popups in real life.
You got /4 concepts.
Write a simple code snippet to dismiss an alert and verify the alert text is 'Confirm delete?'.
Use driver.switchTo().alert() methods.
You got /4 concepts.
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
Step 1: Understand alert interaction methods
The accept() method is used to click the OK button on an alert popup.
Step 2: Differentiate from dismiss()
The dismiss() method clicks Cancel, so accept() must click OK.
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
Step 1: Understand alert lifecycle after accept()
Once accept() is called, the alert is closed and no longer available.
Step 2: Calling dismiss() after accept() is invalid
Trying to dismiss the same alert after accepting it causes an error because alert is gone.
Final Answer:
Cannot call dismiss() after accept() on the same alert instance -> Option D
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
Step 1: Check alert text for "Delete" substring
Use alert.getText().contains("Delete") to detect if alert text includes "Delete".
Step 2: Dismiss alert if condition true, else accept
If text contains "Delete", call dismiss() to click Cancel; otherwise, call accept().
Final Answer:
Alert alert = driver.switchTo().alert();
if (alert.getText().contains("Delete")) {
alert.dismiss();
} else {
alert.accept();
} -> Option C
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 unknownstrongwhile earning +1 Grip toward your certificate.