Continuous Test Orchestration And Execution Platform Online

Perform automated and live-interactive testing on 3000+ real desktop and mobile devices online.

How do I handle alerts, pop-ups, and other web page elements in Selenium?

The purpose of alerts on a webpage is to capture the user's attention. Sometimes, users must input an operation to address alerts on a webpage. On the other hand, popups are windows that appear when you perform a specific action on the web application. You may encounter these when downloading a file or logging into a password-protected website.

Handling alerts in Selenium

There are three types of alerts in Selenium.

  • Simple Alert: The alert() method shows a message box with an "OK" button incorporated into the dialogue window. This approach is used to notify the target user of any specific operational details or modifications that have happened.
  • Prompt Alert:The prompt alert is used to extract information from the user in textual form. It has a text bar where the user can enter their values.
  • Confirmation Alert:This type of alert is dependent on the probability of the dialogue box information being true or false. It is integrated with two commands, "OK" or "Cancel." The user determines the accuracy of the information by clicking on "OK" or "Cancel."

You can use the following methods to handle alerts in Selenium.

  • dismiss():This method is used when you press the ‘Cancel’ button in the alert box.
  • driver.switchTo().alert().dismiss();
  • accept():This method is used when you click on the ‘OK’ button in the alert box.
  • driver.switchTo().alert().accept();
  • sendKeys():It assigns a value to the prompt and can handle all JavaScript-based alerts.
  • driver.switchTo().alert().sendKeys("LambdaTest");
  • getText():It is invoked to capture the alert message.
  • driver.switchTo().alert().getText();

Handling pop-ups in Selenium

When it comes to handling pop-ups in Selenium, there are the following methods.

  • driver.getWindowHandles(): To handle the open windows, you can use driver.getWindowHandles() to switch between them.
  • driver.getWindowHandle(): When the webpage loads, you can use driver.getWindowHandle() to control the main window.
LambdaTest

Test your websites, web-apps, or mobile apps seamlessly with LambdaTest.

Start Free Testing
LambdaTest

Earn resume-worthy Selenium certifications that help you land a top job.

Learn More
LambdaTest

Test your web or mobile apps

Test orchestration and execution cloud of 3000+ browsers and OS

Support

24/7 support

Security

Enterprise grade security

Cloud

Fastest test execution cloud