Alert Handling in Web Testing

How to handle an alert when it is in a new window and if the window is not loaded fully. This window never loads successfully until unless accept/decline the alert.

What have you tried? What happened?

I’m not sure this will work… try calling switchToWindow* then immediately handle the alert


I tried all of these “switchToWindow”. In the console window, I can see it is working still “switchToWindow” but after that not able to accept the alert.

Can you click on the OK button in the console window instead?

No. After “switchToWindow” the click also not working.

1 Like

A couple ideas:

1.) After switching to the new window, do another switch to the alert modal and accept it:

// switch to new window first... then:

WebDriver driver = DriverFactory.getWebDriver();

2.) Use desired capabilities to ignore all alerts:

Add this as an argument to your desired capabilities:


Here’s more info on how to add desired capabilities: