Venkatesh Lakshmanan
Venkatesh Lakshmanan

Reputation: 219

Selenium - Accepting the Confirmation dialog box working fine in Firefox but not in Chrome

Did any one has overcome the issue below?

I have a confirmation dialog box after hitting a submit button. When I use driver.switch().alert().accept() for the Firefox driver, I'm not seeing any issue. But when I use the same script for the Chrome driver, nothing happens. Could you please suggest how to resolve this issue?

I have tried the getWindowHandle() method, but that also didn't work.

Why is it working for Firefox, but not for Chrome?

Upvotes: 2

Views: 16612

Answers (1)

Sriram
Sriram

Reputation: 449

When Working with Alert, i guess its better to use "try/Catch" though it isnt the conventional. I had the same issue with IE. Alert handling was ok with Firefox but failed in IE. You could do something this way

try {
        Alert alert = driver.switchTo().alert();
        String AlertText = alert.getText();
        System.out.println(AlertText);
        alert.accept();
    } catch (Exception e) {
        System.out.println("no alert")
    }

Something like above.

UPDATED

public void aLert() {
    boolean a = false;
    try {
        Alert alert = driver.switchTo().alert();
        String AlertText = alert.getText();
        System.out.println(AlertText);
        alert.accept();
        a = true;
    } catch (Exception e) {
        a = false;
    }finally {
        if (a != true) {
            // take ur screenshot or whatever
            driver.findElement(
                    By.xpath(//xpath of the ok button or accept button)click();
        }
    }

}

Upvotes: 1

Related Questions