python - 如何消除阻止网页内容的“接受 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)
解决方案
如果您不想为 cookie 弹出窗口创建 UI 句柄,您可以在浏览器的选项级别设置一个参数:
options.add_experimental_option("prefs", {"profile.default_content_setting_values.cookies": 2})
推荐阅读
- yocto - 如何在包含目录 yocto 中安装文件
- javascript - 使用 Cheerio Js 选择具有相同类的特定元素
- java - Maven 不会在所有提供的存储库中寻找依赖项
- php - Bootstrap 未与 Laravel 8 集成
- python - 如何使用 Python 和 Selenium 通过 cookie 协议页面?
- r - R中的左对齐非制表符缩进
- sql - Spark SQL - 将列聚合到字典中
- python - Discord Bot Py 无法踢出会员
- c++ - 有人可以解释这段代码的含义吗?
- json - JSON 解析错误:运行 curl 语句以将文件从 EC2 上传到海王星数据库时出现意外字符