How to click the 'Ok' button inside an alert window with a Java Selenium command?
Last Updated :
03 Oct, 2024
An alert window is a pop-up box shown over the webpage to convey error messages using the alert() function of JavaScript. It has only one button "OK" which the user must press to continue browsing the webpage. A person might automate the website testing process and stumble upon pages that display alert messages and have to acknowledge the alert box to continue the testing process. In this tutorial, we will be using Selenium and Java to press the OK button inside the alert window.
Before beginning we have to create a Java Project with or without build tools (Maven/Gradle) to use Selenium.
Steps to create a simple Java Project for Selenium
1. You must have a well-known IDE such as VS Code, IntelliJ, or Eclipse. In this article, we will be using VS Code. Install all the necessary Java extensions suggested by Microsoft to run Java applications and create Java projects.
2. Create and open a folder and open the command palette inside VS Code using Ctrl+Shift+P and type Java: Create a new project and provide the project name.
Folder structure3. Download the latest Selenium package for Java from the official website. Extract the downloaded zip file inside /lib folder of our project.
4. Now we are ready to write code inside the /src/App.java file. Also, you must have the respective driver for your browser.
Steps to click the OK button inside the Alert window
It is important to acknowledge the presence of an alert window by pressing the OK button before trying to do any other tasks such as get page title, element, etc. otherwise we will get UnhandledAlertException. In order to press the OK button inside the alert window, we must also confirm if the alert window is actually present. Otherwise, selenium will throw NoAlertPresentException. Hence we must handle this exception using try-catch blocks.
Using switchTo().alert().accept() method
Java
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class App {
public static void main(String[] args) throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\Drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://globbertrot.github.io/alertpage");
try {
driver.switchTo().alert().accept();
System.out.println(driver.getTitle());
} catch (NoAlertPresentException e) {
System.out.println(driver.getTitle());
}
driver.quit();
}
}
Explanation
- We are using switchTo().alert() method of the driver object which changes the context or focus of the WebDriver from entire webpage to the alert window.
- It returns an Alert object which has many methods such has accept(), dismiss() (in case of confirm box), sendKeys() (in case of prompt box).
- We can use the accept() method in order to press the OK button.
- We are also handling the NoAlertPresentException which is thrown if we are trying to access alert box which is not even present.
Output
Conclusion
Handling alert pop-ups in Selenium Java is essential for dealing with unexpected prompts during test execution. By using the Alert interface, you can easily interact with alert windows and manage actions like clicking the "Ok" button. Mastering this technique ensures that your test scripts handle unexpected pop-ups efficiently, leading to more reliable and stable automated testing.
Similar Reads
How to check if any Alert exists using Selenium with Java? Checking if an alert exists using Selenium with Java is a common scenario when testing web applications. Alerts are pop-up windows generated by the browser to notify users of important messages, such as errors, warnings, or confirmations. An alert window is a pop-up box that is shown over the webpag
4 min read
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 do I send a DELETE keystroke to a text field using Selenium with java? Sending a DELETE keystroke to a text field using Selenium with Java is a common task when automating web form inputs. Whether you're testing the behavior of input fields or simulating user actions, it's important to know how to interact with text fields effectively. Selenium provides a simple way to
3 min read
How to get text from the alert box in java Selenium Webdriver? 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
2 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
How to click a button on webpage using Selenium? This article is all about how to click any button using Selenium on a webpage and many more concepts related to the same which are discussed below. Table of Content What is Selenium? How to click on a button using Selenium Conclusion Frequently Asked Questions on How to click a button on webpage usi
2 min read