我已经花了一周时间试图弄清楚如何消除不断冻结我的代码的“接受cookies”弹出窗口。我从here借用了代码,并对其进行了一些修改以满足我的需求。我搜索了该网站,但几乎没有任何内容看起来像我的情况。我发布了导致问题的代码部分。任何帮助将不胜感激。
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)
而不是
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()
用这个
WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((
By.XPATH,"//div[@id='appconsent']//iframe")))
WebDriverWait(driver,10).until(EC.visibility_of_element_located((
By.CSS_SELECTOR,'button.button__acceptAll'))).click()
如果您不想为 cookie 弹出窗口创建 UI 句柄,您可以在浏览器的选项级别设置参数:
options.add_experimental_option("prefs", {"profile.default_content_setting_values.cookies": 2})
我认为最简单的方法是使用像这样的 chrome 扩展:
它会阻止很多弹出窗口,您无需接受它们。