Sobes.tech
Middle

How to handle pop-ups or alerts in Selenium WebDriver?

sobes.tech AI

Answer from AI

In Selenium WebDriver, the Alert interface is used to work with pop-ups and alerts. To switch to an alert, you need to call the method driver.switchTo().alert(). After that, you can accept the alert (accept()), dismiss it (dismiss()), get its text (getText()), or send text to a prompt (sendKeys()).

Example in Java:

Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
alert.accept(); // click OK

If the alert does not appear immediately, it is recommended to use explicit waits (WebDriverWait) to wait for its appearance.