Trying to 'Accept cookies' by switching to different frame/window

Question:

I am trying to write some python code which can click on ‘Alles accepteren’.
The website is called: www.Bol.com

Because of my lack of knowledge, i don’t know how to find the frame python should focus on.

I know that i should use:

driver.switch_to.frame()

Anyone who can help me??

Asked By: Dion

||

Answers:

Actually, there are no frames on this page. So no need to switch.

element = driver.find_element(By.XPATH, "//button[@id='js-first-screen-accept-all-button']")
element.click()
Answered By: Ivan

There is no iframe, you can just use ID:

driver.find_element(By.ID, "js-first-screen-accept-all-button").click()
Answered By: AbiSaran

You have to just accept the cookies and ever more reliable is to use load time locator strategy which is WebDriverWait

Full working code as an example:

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager

options = webdriver.ChromeOptions()
options.add_argument("--no-sandbox")
options.add_argument('--disable-blink-features=AutomationControlled')
options.add_argument("start-maximized")
options.add_experimental_option("detach", True)

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()),options=options)   

URL ='https://www.bol.com/nl/nl/'
driver.get(URL)

#To accept cookie
WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#js-first-screen-accept-all-button'))).click()
Answered By: Fazlul
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.