Python Forum

Full Version: Generic If Popup Exists Close It Script
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey guys, I'm working on a small script that will check if there's more than 1 window (detect if there's a popup), and if it finds one it will switch to and close it down.

Here's what I have so far:

from selenium.common.exceptions import NoSuchWindowException

if len(driver.window_handles) > 1:
    try:
        driver.switch_to.window([2])
        driver.close()
        driver.switch_to_default_content()        
    except NoSuchWindowException:
        pass
The thing is the popups are happening inside the original window (on page popups), and so the script does nothing lol.

The script ideally needs to close a wide range of popups (I can't easily target by class, or ID because the script encounters many different popups).. it's most important to block the lightbox style squeeze page popups.

At this point I'm thinking of just writing 50 if statements that search for the most common close popup elements.

What you guys think?
Its hard to say without seeing the code, knowing the site, or html.