pythonselenium-webdriverweb-scraping

How to switch to a popup cookie consent page?


I'm using Python 3.12.3, Selenium 4.31.0, Firefox driver in Ubuntu 24.04. When I try to open an url, a cookie consent popup, asking to continue without accepting, accept and more options. How can I switch to the popup and click on continue without accepting, so the popup can be closed and continue to the main content? I'm new to this, can someone confirm if the popup is an alert, a frame or something else? Based on what I see, the tag on the button is

<button type="button" class="iubenda-cs-close-btn" tabindex="0" role="button" aria-pressed="false">Continue without Accepting</button>

and the css path is

html body.absolute-header.has-search-v2.cc-tbp-sdmap.has-widget-bubble.styleTravelBackpack.cc-tbp-fav-tooltip div#iubenda-cs-banner.iubenda-cs-black.iubenda-cs-default.iubenda-cs-bottom.iubenda-cs-overlay.iubenda-cs-slidein.iubenda-cs-no-heading.iubenda-cs-visible div.iubenda-cs-container div.iubenda-cs-content div.iubenda-cs-rationale div.iubenda-banner-content.iubenda-custom-content div#iubenda-cs-title button.iubenda-cs-close-btn

Is it alert or frame or something else?


Solution

  • The popup is HTML. Because you didn't post your code, I can't see what you've tried but a simple WebDriverWait works.

    I've tested that the code below works.

    from selenium import webdriver
    
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.wait import WebDriverWait
    
    driver = webdriver.Chrome()
    
    url = "https://www.costacruises.eu/cruises.html?page=1#occupancy_EUR_anonymous=A&guestAges=30&guestBirthdates=1995-04-10&%7B!tag=destinationTag%7DdestinationIds=ME&%7B!tag=embarkTag%7DembarkPortCode=BCN,ALC,VLC"
    driver.get(url)
    
    wait = WebDriverWait(driver, 10)
    wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#iubenda-cs-title button.iubenda-cs-close-btn"))).click()
    
    driver.quit()