[Python] How to close popup window in Selenium? [closed]

Discussão em 'Python' iniciado por Stack, Março 14, 2025.

  1. Stack

    Stack Membro Participativo

    I've just started learning Selenium, and it's been going great so far. However, I'm stuck on one part. I can handle most popup windows just fine, but I can't seem to get it to work on this particular site. I've tried the usual methods, closing the element directly and switching between the popup and main window but nothing seems to work.

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC

    chrome_options = Options()
    chrome_options.add_experimental_option('detach', True)

    driver = webdriver.Chrome(options=chrome_options)
    url = 'https://www.absoluteboutiquefitness.com.sg/classtype/absolutepilates'
    driver.get(url)

    try:
    popup = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.CLASS_NAME, 'sqs-popup-overlay-close')))

    iframe_list = driver.find_elements(By.TAG_NAME, "iframe")
    if iframe_list:
    driver.switch_to.frame(iframe_list[0])

    # Find and click the close button
    close_button = WebDriverWait(driver, 5).until(
    EC.element_to_be_clickable((By.XPATH, '//*[@id="yui_3_17_2_1_1741961612414_494"]'))
    )
    close_button.click()

    driver.switch_to.default_content()

    except Exception as e:
    print(f"Popup not found or could not be dismissed: {e}")


    I have tried the above but just couldn't get pass it. Including using .sendkeys(Keys.ESCAPE)

    Continue reading...

Compartilhe esta Página