[Python] EXERCISE: Not able to click the deny_cookies_button

Discussão em 'Python' iniciado por Stack, Agosto 4, 2025.

  1. Stack

    Stack Membro Participativo

    I’m writing because I’ve hit a roadblock with a mini‑project. Despite asking ChatGPT for help and trying many different strategies—such as accessing preceding elements, switching iframes before clicking, adding delays, etc.—I’m still stuck and unsure how to proceed.

    I recently learned Selenium through a tutorial and started this project to practice what I learned and build something useful at the same time. I’m scraping easyjet.com, and so far everything is working—except for one final hurdle: clicking the “Deny Cookies” button on the second GDPR/cookie consent popup.

    I’ll include my full code so you can attempt to click the element yourself and explain how you do it. Any suggestions would be greatly appreciated.

    Thank you for your help!

    BASE_URL = 'easyjet.com'


    EASYJET
    |
    |-run.py
    |-EasyJet (folder)
    |-easyjet.py


    run.py:

    from Easyjet.easyjet import EasyJet
    with EasyJet(teardown=False) as bot_easyjet:

    try:
    bot_easyjet.land_home_page()
    bot_easyjet.reject_cookies()
    bot_easyjet.origin_and_destination(origin='Zurich', destination='Madrid')
    bot_easyjet.deaparture_dates(date='5-9-2025')
    bot_easyjet.return_date(date='10-9-2025')
    bot_easyjet.passengers(2,0,0)
    bot_easyjet.submit_info()
    bot_easyjet.reject_second_cookies()
    except Exception as e:
    raise


    easyjet.py

    from selenium import webdriver
    from selenium.webdriver.chrome.service import Service
    from webdriver_manager.chrome import ChromeDriverManager
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.keys import Keys
    import EASYJET.constants as const
    import time
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC


    class EasyJet(webdriver.Chrome):
    def __init__(self, teardown=False):
    self.teardown = teardown
    #prevent google log-in messages
    options = webdriver.ChromeOptions()
    options.add_experimental_option('excludeSwitches', ['enable-logging'])
    super(EasyJet,self).__init__(service=Service(ChromeDriverManager().install()),
    options=options)
    self.implicitly_wait(15)
    self.maximize_window()


    def __exit__(self, exc_type, exc, traceback):
    if self.teardown:
    self.quit()


    def land_home_page(self):
    self.get(const.BASE_URL)


    def reject_cookies(self):
    decline_element = self.find_element(
    By.ID, 'ensRejectAll'
    )
    decline_element.click()

    def origin_and_destination(self, origin:str, destination:str):
    origin_element_box = self.find_element(
    By.ID, 'from'
    )
    origin_element_box.clear()
    origin_element_box.click()
    origin_element_box.send_keys(f'{origin}')

    select_first_element = self.find_element(
    By.CSS_SELECTOR, 'label[data-testid="airport-label"]'
    )
    select_first_element.click()


    destination_element_box = self.find_element(
    By.ID, 'to'
    )
    destination_element_box.click()
    destination_element_box.send_keys(f'{destination}')
    select_first_element = self.find_element(
    By.CSS_SELECTOR, 'label[data-testid="airport-label"]'
    ).click()


    def deaparture_dates(self, date:str):
    select_departure_element_box = self.find_element(
    By.ID, 'when'
    )
    select_departure_element_box.click()

    select_departure_day = self.find_element(
    By.CSS_SELECTOR, f'button[data-testid="{date}"]' #day-month-year; ej: 2-9-2025
    )
    select_departure_day.click()

    def return_date(self, date:str):
    select_departure_day = self.find_element(
    By.CSS_SELECTOR, f'button[data-testid="{date}"]'
    )
    select_departure_day.click()


    def passengers(self, adults:int, children:int, infants:int):
    select_passengers_element_box = self.find_element(
    By.ID, 'who'
    )
    select_passengers_element_box.click()

    adults_on_trip = self.find_element(
    By.CSS_SELECTOR, 'button[aria-label="Add one adult"]'
    )
    for adult in range(adults - 1):
    adults_on_trip.click()



    apply_changes_button = self.find_element(
    By.CSS_SELECTOR, 'button[data-testid="close-button"]'
    )
    apply_changes_button.click()


    def submit_info(self):
    submit_button = self.find_element(
    By.CSS_SELECTOR, 'button[data-testid="submit"]'
    )
    submit_button.click()


    def reject_second_cookies(self):
    deny_button = self.find_element(
    By.CSS_SELECTOR, 'button[data-testid="uc-deny-all-button"]'
    )
    deny_button.click()

    Continue reading...

Compartilhe esta Página