Web alert handling

How to handle alert in if condition
Accept alert only If alert exists that too only when it matches the given alert text.
Please help me

I think that’s the tricky part. I don’t know that there is a mechanism in selenium/webdriver that supports that.

I recall many year ago, I wanted to do something similar and couldn’t find a way. I eventually realized, I was thinking about the problem the wrong way. There were prescribed conditions where the alert WOULD appear along with prescribed conditions where the alert would NOT appear. What I did was test BOTH scenarios and the problem I thought I had went away.

I’d be interested to see if @Brandon_Hein has a method to handle “if-alert-then-verify”

You can get the text from the alert using this method:

It depends on the origin of the alert:

1.) If it’s a browser level alert, there’s no way (that I know of) to handle it conditionally. But that’s ok, that’s what Desired Capabilities are for. Using those, you can ensure that you never get the alert in the first place.
2.) If it’s an alert generated from the application (and thus based on HTML), then you can check for its presence and handle it conditionally, just like any other web element.

At the end of the day, we need to see an example of the alert(s) you are trying to handle.

Is that possible to write
WebUI.verifyEqual(AlertText, ActualAlertText)

Still error occurs when alert not displayed it’s showing alert not found… Is there any way to skip when alert not present

I’m using alert of Adobe should be installed in done machines alerts shown in other not shown so how to handle this


if(WebUI.verifyAlertpresent(10, FailureHandling.OPTIONAL)) { ...


1 Like