1
votes

I am trying to learn selenium and uses firebug to inspect elements on the page. However, whenever there is any pop up or alert message, I am not able to inspect it as it does not allow me to click on inspect element without clicking OK on the popup. I need to automate this clicking of OK button but not able to identify the object properties.

2

2 Answers

2
votes

Actually you can not do inspect element for Alerts , So you can handle those alerts in following way :

            //WAIT UNTIL ALERT OPENS AND THEN CLICK ON OK/ACCEPT
            WebDriverWait wait = new WebDriverWait(driver, 30);
            wait.until(ExpectedConditions.alertIsPresent());
            Alert alert = driver.switchTo().alert();
            alert.accept(); //ACCEPT ACTION

            //WAIT UNTIL ALERT OPENS AND THEN CLICK ON CANCEL/REJECT
            WebDriverWait wait = new WebDriverWait(driver, 30);
            wait.until(ExpectedConditions.alertIsPresent());
            Alert alert = driver.switchTo().alert();
            alert.dismiss(); //REJECT ACTION

Above I am using explicit wait which will wait for alert till 30 seconds. Then I am switching to alert box and at last just accepting/rejecting that alert action.

1
votes

you can not inspect brwoser alert , i made some cute class to handel browser alert like so

import org.openqa.selenium.Alert;
import org.openqa.selenium.UnhandledAlertException;
import org.openqa.selenium.WebDriver;

/**
 *
 * this class for all methods we are used to handle browser alert.
 */
public class BrowserAlertHelper {

    // this is method to check if there any browser alert.
    public static boolean isDialogPresent(WebDriver driver) {
        try {
            driver.getTitle();
            return false;
        } catch (UnhandledAlertException e) {
            // Modal dialog showed
            return true;
        }
    }

    //this is method to accept browser alert.
    public static void acceptBrowserAlert(WebDriver driver) {
        Alert alert = driver.switchTo().alert();
        alert.accept();
    }

    //this is method to decline browser alert.
    public static void declineBrowserAlert(WebDriver driver) {
        Alert alert = driver.switchTo().alert();
        alert.dismiss();
    }

    // get alert text
    public static String getBrowserAlertText(WebDriver driver){
        try {
            Alert alert = driver.switchTo().alert();
            String alertText = alert.getText();
            return alertText;
        } catch (Exception e) {
            System.out.println("no browser alert showing");
        }
        return null;
    }
}

just call the needed method and sent you webdriver as parameter