How to handle Random popups in Selenium WebDriver ?

7,599 views
Skip to first unread message

Sumit Mishra

unread,
Jun 23, 2014, 4:01:56 AM6/23/14
to seleniu...@googlegroups.com

Hello,

When running my testscripts, sometimes an popups appears. The problem is that the popup is totally random and I have no way of knowing when it's going to appear.

I need to know what the popups says, so this can be fixed. Can someone tell me how to handle such an popup, so that i can see what the popup says?

Thanks
Sumit Mishra

DongXiang

unread,
Jun 23, 2014, 4:57:36 AM6/23/14
to seleniu...@googlegroups.com
what kind of popup it is? if it is alert/confirm/prompt, you can override those popup by writing javascript (via web driver) to your UTA page, you can disable those popup and retrieve the content in the popup and writing it to an page element (you may dynamic generate an html element and attach it to the end of html body, the javascript can be injected by web driver).
before run each test step, retrieve the content from this page element, you will get content of it.

window._alert = window.alert;
window.alert = function(str) {
     document.getElementById('popup_content').value=str;
     return false;
}

But something should be aware of, with this javascript, all your alert dialog will not displayed.

I didn't test your case, but above javascript should works.

Best Regards,
--david


Date: Mon, 23 Jun 2014 01:01:56 -0700
From: sumit.m...@gmail.com
To: seleniu...@googlegroups.com
Subject: [selenium-users] How to handle Random popups in Selenium WebDriver ?
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/fd4501c2-d8d2-44bb-8172-af098ff8cb15%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Oscar Rieken

unread,
Jun 26, 2014, 12:15:02 PM6/26/14
to seleniu...@googlegroups.com
if you are testing a site you should probably know the mechanics behind the popup. I would probably talk to your team and see when and why it happens then you will know when you have to check for it or how to avoid it


Amrita B

unread,
Mar 30, 2015, 3:37:37 AM3/30/15
to seleniu...@googlegroups.com
Hi David,

I am facing similar issue.. Could you please let me know code if it is an alert, something like this: alert("You have a new message");

Xiang Dong

unread,
Mar 30, 2015, 4:34:12 AM3/30/15
to seleniu...@googlegroups.com
Hi,

In general, web driver will not notify your test code there is an alert dialog popup. the only way is try switch to the alert dialog, if no alert present, an exception will throw out. You can try catch the codes and continue check before any other web driver actions.

My codes below is override the basic behavior of native dialog, it will make the alert dialog not display.

--david


Date: Mon, 30 Mar 2015 00:35:42 -0700
From: amri...@gmail.com
To: seleniu...@googlegroups.com
Subject: Re: [selenium-users] How to handle Random popups in Selenium WebDriver ?

Mukesh otwani

unread,
Mar 31, 2015, 12:35:06 PM3/31/15
to seleniu...@googlegroups.com
Hi Amrita and David,

We can create a while statement which check if alert is present then it will capture alert message and close alert window.

Try below code and let me know if you finding any issue.

Xiang Dong

unread,
Apr 1, 2015, 2:29:50 AM4/1/15
to seleniu...@googlegroups.com
sure it is a way, but it make the codes complicated, programmer need to check alert present and catch the exception before perform any action, in spite of it, it is not 100% percent works because between the check alert and perform action, there are still some time gap. alert dialog still has possibility to popup. the other way is capture exception for any web driver action, if there is an alert dialog, close it and re-perform the action.

considering it is a random alert dialog, there is no way to anticipate when the alert dialog popup. so, a dedicate logic to handle it is necessary.

Best Regards,
--david


Date: Tue, 31 Mar 2015 09:35:06 -0700
From: mukesh....@gmail.com
To: seleniu...@googlegroups.com
Subject: Re: [selenium-users] How to handle Random popups in Selenium WebDriver ?

Amrita B

unread,
Apr 2, 2015, 6:03:13 AM4/2/15
to seleniu...@googlegroups.com
Hi David,

Could you please let me know the "the other way is capture exception for any web driver action, if there is an alert dialog, close it and re-perform the action."

Also, do we have any way to default behaviour of alert() like we have way for window.alert (  ((JavascriptExecutor)driver).executeScript("window.alert = function(msg){};");)

Actually, I had created code for checking alert and take necessary actions. I have placed it at numerous places in the script but each time it executes it takes time. Also, there's no surety that at other places, message will not come.

I am really tired of this one..

Thanks!

--
You received this message because you are subscribed to a topic in the Google Groups "Selenium Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/selenium-users/Kt0lHgr4qf8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to selenium-user...@googlegroups.com.

To post to this group, send email to seleniu...@googlegroups.com.

tulsi.tester

unread,
Apr 2, 2015, 11:38:55 PM4/2/15
to seleniu...@googlegroups.com
Hello,

If it is a java script alert we can create a function to check whether an alert is present or not and call this function before each line of your webdriver script as you doesn't have any information when an alert pops up.


Function to check alert is present or not:
---------------------------------------------------------

public static Alert alert=null;   //declare this a global variable

public boolean isAlertPresent(){

boolean ispresent = false;

                 try{

                        alert = driver.switchTo().alert();
                        ispresent= true;
                     }catch(NoSuchAlertException e){
                         ispresent = false;
                    }
}


Call the above function before each line of your webdriver code and accept the alert. After accepting the alert again assign alert to null;

Xiang Dong

unread,
Apr 3, 2015, 1:41:54 AM4/3/15
to seleniu...@googlegroups.com
your alert dialog is random, you may have three ways to handle it

1. check alert present or not before perform any web driver based action, such as click, but I think you may have a lot of places to add check, it will be a big effort

2. try catch all your web driver action, if there is an unexpected alert present, you will get exception "UnhandledAlertException", catch this exception and accept the alert, then perform your action again.

3. Override windows.alert function, make your web page totally not display it, you need to add this javascript into the web page, web driver can inject it for you. 

Both three ways all have some weakness, the 1 & 2 needs you fix the codes in a lot of places. A fast way for it is that you can implement a new class inherit from WebDriver, WebElement and other web driver classes. add your alert checking code in each web driver actions, includes findElement or methodes. the third one does not requires much codes but it will override alert function, it also includes the intentional alert dialog. 

Hope it can help you.
--david


Date: Thu, 2 Apr 2015 15:32:44 +0530

Subject: Re: [selenium-users] How to handle Random popups in Selenium WebDriver ?

Oscar Rieken

unread,
Apr 3, 2015, 9:26:13 AM4/3/15
to seleniu...@googlegroups.com
4. is not make the alert random. if the system is yours nothing should happen randomly, work with your team to find out what causes the popup, then go from there, your tests should be deterministic


if you need tests around the popup, then create a state where the pop up happens consistently. 
if you dont need to test the popup, then create a state where it does not pop up

Rajiv Nanduani

unread,
Apr 3, 2015, 12:36:41 PM4/3/15
to seleniu...@googlegroups.com

Amrita B

unread,
Apr 10, 2015, 3:13:32 AM4/10/15
to seleniu...@googlegroups.com
David,

I had tried the 3rd option but as I mentioned earlier it is not working correctly for alert() function. It works for window.alert() though.

Amrita B

unread,
Apr 10, 2015, 3:14:46 AM4/10/15
to seleniu...@googlegroups.com
Yes, I had discussed to switch off a functionality that displays alert messages. I have avoided it for now but not sure if I can encounter random pop ups in other areas..

Amrita B

unread,
Apr 10, 2015, 3:15:13 AM4/10/15
to seleniu...@googlegroups.com
Could you please elaborate on how I can use this especially when I am using a framework for testing.

Devendra Singh

unread,
May 31, 2018, 7:59:19 AM5/31/18
to Selenium Users
Hi Mukesh ,

Same issue i'm facing for unwanted/unexpected pop-up(Bootstrap unwanted pop-up) that occurs .
Your article can be helpful place when we are sure or expected that pop-up might comes .

If any other recommendation or code .Please help out .

Rajeev Rahi

unread,
May 31, 2018, 9:03:27 AM5/31/18
to seleniu...@googlegroups.com
Try to use Robot class. It is able to handle even windows popups.



sruthired...@gmail.com

unread,
Jul 20, 2020, 7:24:06 AM7/20/20
to Selenium Users
hi, i'm facing the similar issue to handling random popups ,i tried one code some person explain previously but its showing errors, any one having idea ,please help me

Claudia Hamm

unread,
Jun 14, 2022, 4:39:12 AM6/14/22
to Selenium Users
How to edit a not selenium default alert pop-up in selenium/python? no such alert problem

I have a big problem with the pop-up window, which is clearly not a default alert message in selenium. I've searched everything but there is no solution for the selenium and python problem. I ask for help. I want to edit and click the following popup.

This pop up is not selenium alert

driver.switch_to.alert() does not work. As I said, it is not a selenium alert. This is error wich shown up when i`m using driver.switch_to.alert : selenium.common.exceptions.NoAlertPresentException: Message: no such alert
citrix.PNG
Reply all
Reply to author
Forward
0 new messages