Python Forum
Generic If Popup Exists Close It Script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Generic If Popup Exists Close It Script
#1
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?
Reply
#2
Its hard to say without seeing the code, knowing the site, or html.
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Selenium click on popup button??? GuJu 7 7,725 Jul-20-2019, 09:21 AM
Last Post: Nizam
  Issue closing an Modal Alert (popup) leviathan54 8 5,821 Apr-23-2019, 02:00 PM
Last Post: leviathan54
  how to make my product description fetching function generic? PrateekG 10 5,950 Jun-29-2018, 01:03 PM
Last Post: PrateekG
  popup login form in django uditvashisht 0 3,778 Jan-31-2018, 11:55 PM
Last Post: uditvashisht
  selenium bypass javascript popup box metulburr 6 8,305 Jun-02-2017, 07:15 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020