How to get text from the alert box in java Selenium Webdriver?
Last Updated :
01 Oct, 2024
In Automation testing, we can capture the text from the alert message in Selenium WebDriver with the help of the Alert interface. By default, the webdriver object has control over the main page, once an alert pop-up gets generated, we have to shift the WebDriver focus from the main page to the alert with the help of switchTO( ).alert( ) method. Once the driver focus is shifted, we can obtain the text of the pop-up with the help of the technique switchTo( ).getText ( ).
Example to get text from the alert box in Java Selenium Webdriver
Take an example of the alert below and its message.
Site URLSyntax
Alert al = driver.switchTo().alert();
String st= driver.switchTo().alert().getText();
al.accept();
Here is the code to get text from the alert box in java Selenium Webdriver:
AlertMessage.java
package seleniumpractice;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class AlertMessage {
public static void main(String[] args) throws Exception
{
System.setProperty(
"webdriver.chrome.driver",
"F:\\ Maven Selenium\\selenium\\src\\main\\java\\seleniumpractice\\chromedriver.exe"); // Replace the Path in which your Chrome Webdriver stay
WebDriver driver = new ChromeDriver();
// implicit wait
driver.manage().timeouts().implicitlyWait(
5, TimeUnit.SECONDS);
// URL launch
driver.get(
"https://the-internet.herokuapp.com/javascript_alerts"); // Replace the Site As per need
Thread.sleep(1000);
// identify element
WebElement we = driver.findElement(By.xpath(
"//*[text()='Click for JS Alert']")); // Location
// of the
// AlertBox
we.click();
// switch focus to alert
Thread.sleep(1000);
Alert al = driver.switchTo().alert();
// get alert text
String st = driver.switchTo().alert().getText();
System.out.println("Alert text is: " + st);
// accepting alert
al.accept();
driver.quit();
}
}
Explanation
Set Up ChromeDriver:
System.setProperty()
sets the path for ChromeDriver.WebDriver driver = new ChromeDriver();
initializes the browser.- Implicit Wait:
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
waits for elements for up to 5 seconds. - Open URL:
driver.get("https://the-internet.herokuapp.com/javascript_alerts");
navigates to the page with JS alerts. - Locate and Click Element: Finds the element triggering the alert using XPath and clicks it.
- Handle JavaScript Alert:
driver.switchTo().alert()
switches to the alert.getText()
retrieves the alert message.accept()
clicks the "OK" button on the alert.
- Close Browser:
The script demonstrates handling JavaScript alerts in Selenium with key actions: launching the browser, clicking an element to trigger the alert, and interacting with it.
Output
OutputConclusion
Capturing the text from an alert message in Java Selenium WebDriver is a key functionality when automating tests for applications using JavaScript alerts. By accessing the alert using the switchTo().alert()
method and retrieving the text with getText()
, testers can easily verify the alert content. Implementing this approach ensures more comprehensive test coverage and helps maintain the reliability of web application tests.
Similar Reads
How to upload a file in Selenium java with no text box? Uploading a file in Selenium Java without a visible text box can be easily handled using the sendKeys() method. Even if the file input field is hidden or styled as a button, Selenium allows you to interact with it by providing the file path directly. This method simplifies the process of file upload
3 min read
How to check that the element is clickable or not in Java Selenium WebDriver? Ensuring that an element is clickable in your Selenium WebDriver tests is crucial for validating the functionality and user experience of your web applications. In Java Selenium WebDriver, checking if an element is clickable before interacting with it can help avoid errors and ensure that your test
3 min read
How to submit a form in java Selenium webdriver if submit button can't be identified? Submitting a form in Selenium WebDriver can sometimes be challenging, especially when the submit button cannot be easily identified. In such cases, Java Selenium WebDriver offers alternative methods to interact with web elements and successfully submit the form. Whether the submit button lacks a uni
3 min read
How to Select Date from Datepicker in Selenium Webdriver using Java? In the world of web automation, selecting a date from a datepicker is a common task, especially for applications that involve date selection, such as booking systems, forms, and reservations. Selenium WebDriver is a powerful tool for automating web browsers, and when combined with Java, it provides
3 min read
How to Handle Alert in Selenium using Java? Imagine filling out a form online and accidentally missing some information. You only know if you made a mistake if the website tells you somehow, like with a pop-up message. This article explains what those pop-up messages are called in Selenium (alerts) and how to deal with them in your automated
5 min read