0

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:
        try:
            driver.find_element_by_class_name(text)
        except NoSuchElementException:
            break
        else:
            popup_close_button = driver.find_element_by_class_name("close_m")
            popup_close_button.click()

...
check_popups("alert")

account_button = driver.find_element_by_class_name("lk_reg")
account_button.click()

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

2
  • 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. Sep 1 '20 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.
    Sep 1 '20 at 17:46
0

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.

2
  • 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.
    Sep 1 '20 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
    Sep 4 '20 at 17:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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