The following piece of Python Selenium code do not work and I would be grateful for helping me to understand why.

It is designed to close pop-ups that block access to the account button. The number of pop-ups may vary. The idea is execute the while loop unless all the pop-ups are closed (the same function is to be called from other lines of the code as well), than the loop should be broken, so the program execution can continue. But it never does.

def check_popups(text):
    while True:
        except NoSuchElementException:
            popup_close_button = driver.find_element_by_class_name("close_m")


account_button = driver.find_element_by_class_name("lk_reg")

P. S. Names for all classes are correct, they are not the suspects.

  • 1 - Without your application, it will be difficult to assess what's the issue. 2 - Why don't you create a build of your application without such pop-up, since it's not relevant for your testing? Increasing testability is usually preferable than working around testability issues, especially when these issues increase your automated checks run-time and make them more complex unnecessarily. Commented Sep 1, 2020 at 16:19
  • @JoãoFarias You misunderstood me. I have nothing to do with development of this application (I could give the link to this site, but this does not make much sense for you won't be able to log-in unless you work in certain medical institutions). I am forced to work with it and the mentioned pop-ups makes this work quite unpleasant. And this code is not even a test, all I am trying to do is implementing scenario to get rid of the pop-ups which I distaste not unlike almost every one else...
    – S. N.
    Commented Sep 1, 2020 at 17:46

1 Answer 1


It looks like you are trying to handle an alert. You cannot handle alerts in this way. How to Handle Alert & Pop-up Boxes in Selenium Python for some background for example may be what you intend. Alerts are tricky, and thus far I've avoided having them in our application code for this reason. This SO post may be what you want to be doing Check if any alert exists using selenium with python .

A side note: Oh,and welcome to SO, but mostly welcome to the world of Test Automation. I advise against pop-ups, because they are a user-interface anti-pattern. We should avoid alerts in browser apps, because they break the user focus and interactions are interrupted for a user. We want them only for errors. And to this end, mostly you want your developers to write code that is easy to test. So if the code is hard to test quickly, the developers are possibly not helping themselves.

  • 1
    Yes, «alert» seems to be the correct term for what I called «pop-up». Thank you for the links, I'll definitely need more information about the alerts and how to handle them. I totally agree with every thing you have written above about pop-ups/alerts (please check my comment for the original post; I am not the one who favours alerts; actually, I am suffering because of them; my aim is to make life a bit easier for myself and my colleagues). The last but not the least, thank you for your warm welcome!
    – S. N.
    Commented Sep 1, 2020 at 18:03
  • Thanks @S.N. I went a little off topic on this answer mainly because I've not written any alert handlers myself without copy-pasting from SO already. Glad my off-topic guidance to avoid alerts as far possible is well accepted, please do share a code solution or mark this as an answer if it did help.
    – Conrad B
    Commented Sep 4, 2020 at 17:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.