Alert box not getting closed due to WebDriver Error

Hello,

My script to close an alert box is failing with a driver related error, as provided below.
The same script works sometimes like 1 out of 10 attempts.

I’m just using the following code snippet to close the alert. Please let me know if I need to change anything on my approach to close the pop-up.

// Step 13
.then(function(){
log(13, ‘closeAlertBox’);
$browser.sleep(5000);
$browser.switchTo().alert().accept();
$browser.takeScreenshot();
})

Logs :

Step 13: closeAlertBox STARTED at 39010ms.
Browser script execution FAILED.
Script execution failed: WebDriverError: invalid session id
(Driver info: chromedriver=2.46.628388 (4a34a70827ac54148e092aafb70504c4ea7ae926),platform=Linux 5.4.0-1054-aws x86_64)
Script execution failed (due to an unhandled promise rejection): WebDriverError: invalid session id
(Driver info: chromedriver=2.46.628388 (4a34a70827ac54148e092aafb70504c4ea7ae926),platform=Linux 5.4.0-1054-aws x86_64)
Script execution failed (due to an unhandled promise rejection): TypeError: Cannot read property ‘send’ of null
(node:182) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously (rejection id: 1)

Hi there @Karthik.Sridhar -

Is this alert box a pop up window? If that’s the case, then I have a workaround for you. Closing a window is not possible, but you can alter your script to return to the window beneath it:

https://newrelic.zendesk.com/hc/en-us/articles/360048676213-Close-a-window-in-a-Synthetics-script

Let me know if I misunderstood and you are looking for something else.