简体   繁体   English

我如何使用 Selenium “保存并退出”cookie 弹出窗口

[英]How I can "save & exit" cookies popup with Selenium

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.我正在尝试抓取一个转移标记网页部分,我不想接受弹出窗口中的所有 cookie,而是对其进行配置并接受这些更改。 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谢谢

To be able to switch to another iframe you need to switch back to default content first:为了能够切换到另一个 iframe,您需要先切换回默认内容:

...
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"]')))
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM