How to handle modal dialog box in Selenium WebDriver with Java?

How to handle modal dialog box in Selenium WebDriver with Java?

Hi Emma,

We recommed you to check the following blog to learn how you can handle modal dialog box in Selenium WebDriver using Java.

To handle a modal dialog in Selenium WebDriver with Java, you can use explicit wait to wait for the modal to appear, and then switch to the modal dialog window to interact with its elements.

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement modal = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("modal-id")));
// Switch to modal dialog
driver.switchTo().activeElement();
// Perform actions on modal dialog

If the modal dialog is an alert, you can use the Alert interface in Selenium WebDriver to handle it.

Alert alert = driver.switchTo().alert();
// Perform actions on the alert
alert.accept(); // or alert.dismiss();