How I can "save & exit" cookies popup with Selenium

Issue

I’m, triying to scrape a transfermark web section and I don’t want to accept all cookies in popup, but to configure it and accept these changes. I’ve achieve do click on "option" button in first frame, but when second frame appears I can’t click on "save and exit" button. This is my code:

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

url = 'https://www.transfermarkt.es/transfers/transfertagedetail/statistik/top/land_id_zu/0/land_id_ab/0/leihe//datum/2022-07-10'

driver = webdriver.Chrome()
driver.minimize_window()
driver.get(url_base)

wait = WebDriverWait(driver, 30)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, '//*[@id="sp_message_iframe_575430"]')))
wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="notice"]/div[3]/div[1]/button'))).click()
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, '//*[@id="sp_message_iframe_225826"]')))
wait.until(EC.element_to_be_clickable((By.XPATH, '/html/body/div/div[2]/div[5]/button[2]'))).click()

thanks

Solution

To be able to switch to another iframe you need to switch back to default content first:

...
wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="notice"]/div[3]/div[1]/button'))).click()
driver.switch_to.default_content()
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, '//*[@id="sp_message_iframe_225826"]')))
...

Answered By – DonnyFlaw

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published