How Do You Handle an Okay-cancel Popup in Selenium?

Dapheny Murphy Dapheny Murphy | November 7, 2022

How Do You Handle an Okay-cancel Popup in Selenium?

Alerts are tiny pop-up windows or boxes that show messages or notifications and ask the user for permission to perform specific types of operations in exchange for some information. They can also be used as warning techniques.

The alert "okay cancel" popup in Selenium, is a tiny message box that shows up on the screen to provide information or request authorization before carrying out an operation.

The majority of the time, when we fill out a form and click the submit button, the message "phone number is necessary" pops up or we neglect to tick the box for the terms and conditions. Once I have filled all the mandatory fields, the form won't submit until I press the OK button on the alert box.

In Selenium, We Use the Following Methods To Handle Alerts:

  1. Void dismiss(): It is used to click on the ‘Cancel’ button of the alert box.

    driver.switchTo().alert().dismiss();

  2. Void accept(): It is used to click on the ‘OK’ button of the alert.

    driver.switchTo().alert().accept();

  3. String getText(): It is used to capture the alert message.

    driver.switchTo().alert().getText();

  4. Void sendKeys(String stringToSend): It is used to send data to the alert box.

    driver.switchTo().alert().sendKeys("Text");

Schedule a Call

Disclaimer

This publication is for informational purposes only and nothing contained in it should be considered legal advice. We expressly disclaim any warranty or responsibility for damages arising out of this information and encourage you to consult with legal counsel regarding your specific needs. We do not undertake any duty to update previously posted materials.

Post a Comment

We use cookies to optimize user experience. Click on "Agree and Proceed" to confirm, OR, by continuing, you implicitly accept cookies.