I already spend a week trying to figure out how to dismiss the "accept cookies" popup that keeps freezing my code.I borrowed the code from here and transformed it a bit to meet my needs.I searched the site but almost nothing to seem like my case.I m posting the portion of the code that causes the problem.Any help will be appreciated.
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)
CodePudding user response:
Instead of
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()
Use this
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()
CodePudding user response:
If you don't want to create an UI handle for cookies popup, you can set an argument at browser's option level:
options.add_experimental_option("prefs", {"profile.default_content_setting_values.cookies": 2})