Checking If alert exists before switching to it

671 views
Skip to first unread message

Chuck Woodraska

unread,
Apr 21, 2011, 11:57:53 AM4/21/11
to Selenium Users
In Selenium 2, is there a way to click a button and then check if an
alert exists after the button click? If it exists I want to accept the
alert otherwise I would just move on to my next step.

Lutfi Dughman

unread,
Apr 21, 2011, 12:01:00 PM4/21/11
to seleniu...@googlegroups.com
if using webdriver


webelement.click
Alert alert = driver.switchto().alert() 
alert.getText ....




--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.


Chuck Woodraska

unread,
Apr 21, 2011, 12:28:17 PM4/21/11
to Selenium Users
I couldn't get that to work. I'm pretty sure this is because the alert
doesn't exist so it gave me a NullPointerException. I am able to do it
by checking the page title after the element.click to the page title
before the element.click . If the page titles are the same I know the
alert stop it from changing pages and I have to deal with the alert
otherwise I continue running my tests.

On Apr 21, 11:01 am, Lutfi Dughman <lutf...@gmail.com> wrote:
> if using webdriver
>
> webelement.click
> Alert alert = driver.switchto().alert()
> alert.getText ....
>
> On Thu, Apr 21, 2011 at 11:57 AM, Chuck Woodraska <chuck.woodra...@gmail.com

Lutfi Dughman

unread,
Apr 21, 2011, 12:52:15 PM4/21/11
to seleniu...@googlegroups.com
is this browser alert, or some custom javascript alert? because you can't use the alert() with javscript alerts.

Brian Kitchener

unread,
Apr 24, 2011, 10:36:08 AM4/24/11
to Selenium Users
You can wrap anything in a try/catch statement. And then it won't
throw an error.

Chuck Woodraska

unread,
Apr 26, 2011, 8:27:33 PM4/26/11
to Selenium Users
It is a browser alert. Finally got the try/catch to work after I moved
the Alert alert = driver.switchto().alert() ; out and just left the
alert.accept(); in the try.

Yanan

unread,
Apr 25, 2011, 9:41:49 AM4/25/11
to Selenium Users
I have the same problem as John's.
After clicking on a button, there is an alert popup. But I get
NullPointerException when I use driver.switchto().alert().getText().
There is only one window is returned when I use
driver.getWindowHandles().
Any idea?

Thanks,
Yanan

On Apr 24, 10:36 am, Brian Kitchener <kitchener.br...@gmail.com>
wrote:
> You can wrap anything in a try/catch statement. And then it won't
> throw an error.
>
> On Apr 21, 9:57 am, Chuck Woodraska <chuck.woodra...@gmail.com> wrote:
>
> > In Selenium 2, is there a way to click a button and then checkifan
> >alertexistsafter the button click?IfitexistsI want to accept the
> >alertotherwise I would just move on to my next step.

Ctrl+Shft+Eject

unread,
Nov 4, 2011, 6:59:23 PM11/4/11
to seleniu...@googlegroups.com
So far this appears to be working for me, although alert is intermittent. I have run multiple test now without coming across the alert.

 public static boolean assertAndAcceptAlert (String alertText) {
         Alert alert = null ;
         try{ if (alertText.equals(alert.getText())){
                switchTo().alert(); boolean ok = alertText.equals(alert.getText()); alert.dismiss(); return ok;}
            else { return false; }}
         catch (NullPointerException npe){
             Log.state("Caught: " + npe );}
     return true;}

Reply all
Reply to author
Forward
0 new messages