This test opens a page with JavaScript alerts, clicks a button to show an alert, reads and prints the alert text, accepts the alert, then prints the result message on the page.
Selenium Java
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class AlertTest {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("https://the-internet.herokuapp.com/javascript_alerts");
// Click button to trigger alert
driver.findElement(By.xpath("//button[text()='Click for JS Alert']")).click();
// Switch to alert and accept it
Alert alert = driver.switchTo().alert();
String alertMessage = alert.getText();
System.out.println("Alert says: " + alertMessage);
alert.accept();
// Verify result text
String result = driver.findElement(By.id("result")).getText();
System.out.println("Result text: " + result);
driver.quit();
}
}
OutputSuccess
Important Notes
Always switch to the alert before interacting with it.
Accepting confirms the alert; dismissing cancels or closes it.
Alerts block the page until handled, so tests wait for alert actions.
Summary
Use driver.switchTo().alert() to access alerts.
accept() clicks OK, dismiss() clicks Cancel.
Handle alerts to keep tests running smoothly.
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.