[Python] Can't close cookie pop up on website with selenium webdriver

Discussão em 'Python' iniciado por Stack, Abril 26, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to use selenium to click the Accept all or Reject all button on a cookie pop up for the the website autotrader.co.uk, but I cannot get it to make the pop up disappear for some reason.

    This is the pop up:

    [​IMG]

    and here is the html:

    <button title="Reject All" aria-label="Reject All" class="message-component message-button no-children focusable sp_choice_type_13" style="opacity: 1; padding: 10px 5px; margin: 10px 5px; border-width: 2px; border-color: rgb(5, 52, 255); border-radius: 5px; border-style: solid; font-size: 14px; font-weight: 400; color: rgb(255, 255, 255); font-family: arial, helvetica, sans-serif; width: calc(35% - 20px); background: rgb(5, 52, 255);">Reject All</button>


    [​IMG]

    The code I have tried is the following:

    from selenium import webdriver
    from selenium.webdriver.chrome.service import Service
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    import time

    path_to_driver = r"C:\path_to_project\chromedriver.exe"
    service = Service(executable_path=path_to_driver)

    driver = webdriver.Chrome(service=service)
    driver.get("https://www.autotrader.co.uk")
    time.sleep(5)
    WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.CLASS_NAME, 'message-component message-button no-children focusable sp_choice_type_13'))).click()

    time.sleep(10)
    driver.quit()


    Can anyone help here?

    Continue reading...

Compartilhe esta Página