Next-Gen App & Browser
Testing Cloud

Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

Next-Gen App & Browser Testing Cloud

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.

Test Your Website on 3000+ Browsers

Get 100 minutes of automation test minutes FREE!!

Test Now...

KaneAI - Testing Assistant

World’s first AI-Native E2E testing agent.

...
ShadowLT Logo

Start your journey with LambdaTest

Get 100 minutes of automation test minutes FREE!!

Signup for free