How to accept “This action cannot be undone. Are you sure you want to delete this wishlist” popup using Selenium and Java


I'm in need of clicking the first button of a browser notification, which appears after clicking on a button "Delete Wishlist". This button is as below;

  <a href="/account/wishlistupdate?mainAction=delete&amp;wishlistId=2547474" class="btn btn-danger btn-sm" onclick="return confirm('This action cannot be undone. Are you sure you want to delete this wishlist?')" style="" xpath="1">Delete wishlist</a>   

This button click opens a browser notification.

enter image description here

I have tried alert.accept(), but doesn't work for me.

 public HomePage editWishlist(){
    click(edit_button);    //     By edit_button = By.xpath("//div[@class='btn-edit-text']");

    click(deleteWishlist_button); //    By deleteWishlist_button = By.linkText("Delete wishlist");

    try {
        Thread.sleep(1500);
    } catch(InterruptedException e) {
        System.out.println("got interrupted!");
    }

    Alert alert = driver.switchTo().alert();
    alert.accept();

    return this;
}

How can I manage this?


Answers:


This is a Javascript Confirmation Alert and you can accept it by switching to it and then accepting it:

driver.switchTo().alert().accept();

Similarly to click on cancel you can call method dismiss()