pythonseleniumxpathwebdriverwebdriverwait

Python selenium click on cookie popup


I would like to close the cookie popup in a page by clicking the accept button. Please could you help?

driver.get('https://vaccination-covid.keldoc.com/redirect/?dom=cabinet-medical&inst=bain-de-bretagne-35470&user=salle-des-fetes-de-bain-de-bretagne')
time.sleep(4)
btns = driver.find_elements_by_xpath("//button[contains(text(), 'accepte')]")
for btn in btns:
    btn.click()

Solution

  • The accept Button is inside an iframe you need to switch it.

    driver.switch_to.frame("Iframe")
    driver.find_element_by_xpath("//button[contains(text(), 'accepte')]").click()
    

    Ideally you should use WebDriverWait()

    driver.get("https://vaccination-covid.keldoc.com/redirect/?dom=cabinet-medical&inst=bain-de-bretagne-35470&user=salle-des-fetes-de-bain-de-bretagne")
    WebDriverWait(driver,20).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"Iframe")))
    WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//button[contains(text(), 'accepte')]"))).click()
    

    You need to import these libraries:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    

    To jump out from iframe use

    driver.switch_to.default_content()