Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

How to handle web based alerts in Selenium?



Selenium WebDriver gives multiple APIs to handle pop ups or alerts with the help of Alert interface.

  • dismiss()

    This will cancel the button for alert.

  • accept()

    This will accept the button for alert.

  • getText()

    This will extract the text on alert.

  • sendKeys()

    This will enter text on the alert box.

Example

Syntax with code snippet −

// Alert
   Alert a = driver.switchTo().alert();
   // Extract alert message.
   String msg= driver.switchTo().alert().getText();
   // print the message on console
   System.out.println(msg);
   // entering text on alert box
   a .sendkeys(“Testing”);
   // alert accept
   a.accept()
   // alert dismiss
      a.dismiss()
Updated on: 2020-06-10T14:49:12+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements