How to handle alerts in Selenium with Java?

   IHUB Talent: Best Selenium with Java Training in Hyderabad with Live Internship

Looking to build a strong career in automation testing? IHUB Talent offers the best Selenium with Java training in Hyderabad, designed to turn you into a job-ready automation tester with practical, hands-on skills and real-world experience through a live internship program.

At IHUB Talent, the training is led by industry experts who bring in real-time scenarios and case studies to ensure you're learning what truly matters in today’s software testing landscape. The course covers Selenium WebDriver, Java programming basics to advanced concepts, TestNG, Maven, Jenkins, Git, and frameworks like POM and BDD (Cucumber) — everything you need to become a full-stack automation tester.

What makes IHUB stand out is the live internship program where students work on real-time projects with actual companies. This practical exposure gives you the confidence to apply your skills in a real environment and helps you build a professional portfolio that impresses recruiters.

Whether you're a fresher, manual tester, or a working professional looking to upskill, IHUB Talent’s job-oriented training with 1:1 mentorship, resume building, and interview prep is the right choice.

The Actions class in Selenium is used to build and perform complex user interactions like mouse movements, keyboard events, drag-and-drop, and other advanced gestures that go beyond simple clicks or typing.

Handling alerts in Selenium with Java involves interacting with popup windows such as JavaScript alerts, confirmations, or prompts that appear during web automation. Here’s how you can manage them effectively:


1. Switch to Alert

Before interacting with an alert, switch the WebDriver’s context to the alert box using:


java

Copy

Edit

Alert alert = driver.switchTo().alert();

2. Types of Alerts and Actions

Simple Alert: Just an OK button.


java

Copy

Edit

alert.accept();  // Clicks OK

Confirmation Alert: OK and Cancel buttons.


java

Copy

Edit

alert.dismiss();  // Clicks Cancel

// or

alert.accept();   // Clicks OK

Prompt Alert: Allows input text.


java

Copy

Edit

alert.sendKeys("Your input");

alert.accept();

3. Get Alert Text

To read the message shown in the alert:


java

Copy

Edit

String alertText = alert.getText();

System.out.println("Alert says: " + alertText);

4. Example Flow

java

Copy

Edit

// Trigger action that causes alert

driver.findElement(By.id("alertButton")).click();


// Switch to alert

Alert alert = driver.switchTo().alert();


// Read alert text

System.out.println(alert.getText());


// Accept the alert

alert.accept();

5. Handling NoAlertPresentException

If no alert is present, Selenium throws NoAlertPresentException. Handle it with try-catch:


java

Copy

Edit

try {

    Alert alert = driver.switchTo().alert();

    alert.accept();

} catch (NoAlertPresentException e) {

    System.out.println("No alert found.");

}

Summary

Use driver.switchTo().alert() to focus on the alert.


Use accept(), dismiss(), and sendKeys() to interact.


Always handle exceptions to avoid test failures.

Read More


Visit I HUB TALENT Training Institute In Hyderabad

Comments

Popular posts from this blog

What is Selenium and how is it used with Python?

What is an implicit wait?

How to set up Selenium WebDriver with Java?