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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Selenium click on popup button??? GuJu 7 2,824 Jul-20-2019, 09:21 AM
Last Post: Nizam
  Issue closing an Modal Alert (popup) leviathan54 8 2,057 Apr-23-2019, 02:00 PM
Last Post: leviathan54
  how to make my product description fetching function generic? PrateekG 10 2,315 Jun-29-2018, 01:03 PM
Last Post: PrateekG
  popup login form in django uditvashisht 0 2,210 Jan-31-2018, 11:55 PM
Last Post: uditvashisht
  selenium bypass javascript popup box metulburr 6 4,444 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