How to detect or click on multiple javascript alert popups

Hi all

I wanted to know how can i detect javascript alert popup after certain tasks i have used xclick, executeScript, but nothing works

im using website where i want to click on button then website shows javascript alert popup but its random sometimes website shows 1 alert sometimes it shows 2 button so can we handle this? there is multiple button i want like click button one by one but because of random popup count i cant do that so how i can detect popup button.

i want to add functionality click button then close the all js alert button then continue the so on

My code:

{
  "Name": "Save All",
  "CreationDate": "2024-11-24",
  "Commands": [
    {
      "Command": "store",
      "Target": "1",
      "Value": "i",
      "Description": "Initialize counter"
    },
    {
      "Command": "while",
      "Target": "${i} <= 10",
      "Value": "",
      "Description": "Loop through 10 buttons"
    },
    {
      "Command": "click",
      "Target": "xpath=(//button[@id='is_finalized'])[${i}]",
      "Value": "",
      "Description": "Click the nth button"
    },
    {
      "Command": "pause",
      "Target": "500",
      "Value": "",
      "Description": "Wait for alert to appear"
    },
    {
      "Command": "executeScript",
      "Target": "window.alert && alert('');",
      "Value": "",
      "Description": "Close the alert programmatically"
    },
    {
      "Command": "executeScript",
      "Target": "return Number(${i}) + 1;",
      "Value": "i",
      "Description": "Increment counter"
    },
    {
      "Command": "end",
      "Target": "",
      "Value": "",
      "Description": "End of loop"
    }
  ]
}

Hi, can you please add a screenshot or video of the situation?

Thanks for the video. Question: Did you already try a click on the OK button with XClick in desktop mode? It needs to be in desktop mode, because this dialog is outside the browser viewport. That approach works fine in my test:

{
  "Name": "alertpopup",
  "CreationDate": "2024-11-28",
  "Commands": [
    {
      "Command": "open",
      "Target": "https://www.w3schools.com/js/tryit.asp?filename=tryjs_alert",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "XClick",
      "Target": "try2_dpi_168.png",
      "Value": "",
      "Description": "Trigger alert popup for testing"
    },
    {
      "Command": "pause",
      "Target": "2000",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "XDesktopAutomation",
      "Target": "true",
      "Value": "",
      "Description": "Switch to desktop mode"
    },
    {
      "Command": "XClick",
      "Target": "ok_dpi_168.png",
      "Value": "",
      "Description": "Click the OK button of the alert popup"
    },
    {
      "Command": "XDesktopAutomation",
      "Target": "false",
      "Value": "",
      "Description": "Back to browser automation"
    },
    {
      "Command": "XClick",
      "Target": "button_dpi_168.png",
      "Value": "",
      "Description": "Just another inside website button click as demo"
    }
  ]
}