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

16,197

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();
        }
    }

}
Share:
16,197
Venkatesh Lakshmanan
Author by

Venkatesh Lakshmanan

Updated on June 04, 2022

Comments

  • Venkatesh Lakshmanan
    Venkatesh Lakshmanan almost 2 years

    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?

  • Venkatesh Lakshmanan
    Venkatesh Lakshmanan almost 11 years
    No, its not working. The Script execution stops after getting the confirmation window. I tried in Debug mode, the script is not going to the next line if i press F6. But when click Ok/Cancel button in the confirmation window, the script start executes. Any idea?
  • Sriram
    Sriram almost 11 years
    @VenkateshLakshmanan : I guess the Alertbox is not recognized. Try checking the presence of Alertbox in the code. What i have given above is a sample on how to do it.(actually it should work). I have updated the answer to check the presence of Alertbox. Give it a shot and let us know.
  • Venkatesh Lakshmanan
    Venkatesh Lakshmanan almost 11 years
    Hi Sriram, Thanks for helping me out. I'm very new to selenium. Could you please help me how to find the xpath for the Confirmation pop up. Its a modal dialog box. I couldn't able to do anything.
  • Sriram
    Sriram almost 11 years
    @VenkateshLakshmanan : If u are new, learning how to frame xpath or Css is essential. There are many sites/materials online which provide essential informations on those. Since you are new to selenium, apart from xpath there are other ways of identifying elements. For Ex : id,tagname etc. Now to answer your question, if u are using firefox there is something called firebug and firepath (both are addons). You can use the above to get the firepath of the element. For chrome, once you open the browser and your application, press F12. The console module opens.
  • Sriram
    Sriram almost 11 years
    @VenkateshLakshmanan : At the left bottom. U will have a search symbol. Click that(its for selecting an element). Now when u hover yourmouse over the ok/accept button, u can see it getting highlighted.Click the ok/accept button. Now in the console u can see the html snippet of the element getting highlighted. Right click on the HTML and select "copy xpath". Bingo!! U have your xpath now.
  • Venkatesh Lakshmanan
    Venkatesh Lakshmanan almost 11 years
    @sriram.. i'm using Firebug to identify the XPATH.. As this is the modal dialog box how i will use the firebug to identify the element. ?????
  • Sriram
    Sriram almost 11 years
    @VenkateshLakshmanan : The regular way. I mean how you identify other elements. Firebug identifies elements for alerts/modal dialog box as well.