This test opens a page with a prompt alert, enters text, accepts it, and prints the result message.
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class PromptAlertTest {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
WebDriver driver = new ChromeDriver();
try {
driver.get("https://the-internet.herokuapp.com/javascript_alerts");
driver.findElement(By.xpath("//button[text()='Click for JS Prompt']")).click();
Alert alert = driver.switchTo().alert();
alert.sendKeys("Selenium Test");
alert.accept();
String resultText = driver.findElement(By.id("result")).getText();
System.out.println(resultText);
} finally {
driver.quit();
}
}
}