0

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?

Aalia Sajana
  • 53
  • 3
  • 10

2 Answers2

0

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

Mustahsan
  • 3,852
  • 1
  • 18
  • 34
0

This popup with text as

This action cannot be undone. Are you sure you want to delete this wishlist? 

...is the implementation of onbeforeunload property of WindowEventHandlers.


onbeforeunload

The onbeforeunload property of the WindowEventHandlers mixin is the EventHandler for processing beforeunload events. These events fire when a window is about to unload its resources. At this point, the document is still visible and the event is still cancelable.


Solution

A Cross Browser solution would be to disable this dialog invoking the executeScript() setting window.onbeforeunload as function() {}; and you can use the following solution:

    driver.execute_script("window.onbeforeunload = function() {};")

You can find a relevant discussion in How to disable a “Reload site? Changes you made may not be saved” popup for (python) selenium tests in chrome?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352