首页 > 解决方案 > 如何消除阻止网页内容的“接受 cookie”弹出窗口?

问题描述

我已经花了一周的时间试图弄清楚如何关闭一直冻结我的代码的“接受 cookie”弹出窗口。我从这里借了代码并对其进行了一些修改以满足我的需要。我搜索了该网站,但几乎没有任何东西看起来像我的情况。我正在发布导致问题的代码部分。任何帮助将不胜感激。

from selenium import webdriver
from selenium.webdriver.opera.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from time import sleep
from datetime import datetime
import pandas as pd

errors = []
season = []


for id in range(2124889, 2124890):
# Opening the connection and grabbing the page
my_url = f'https://www.lefigaro.fr/sports/football/live/bundesliga/2020/{id}/*'
option = Options()
option.headless = False
driver = webdriver.Opera(options=option)
driver.get(my_url)
driver.maximize_window()
WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((
        By.XPATH,'//iframe[@id="appconsent"]')))
WebDriverWait(driver,10).until(EC.element_to_be_clickable((
        By.XPATH,'//button[contains(@title,"Tout Accepter")]'))).click()
sleep(5)

标签: pythonseleniumpopup

解决方案


如果您不想为 cookie 弹出窗口创建 UI 句柄,您可以在浏览器的选项级别设置一个参数:

options.add_experimental_option("prefs", {"profile.default_content_setting_values.cookies": 2})

推荐阅读