Skip to content Skip to sidebar Skip to footer

Selenium Accept Cookies Python

I am trying to accept cookies on website, sadly I cannot refer to the 'accept all' button. I have directly in the code time.sleep(10) so there should not be a problem with the wait

Solution 1:

The element is in a Shadow root

Try below code.

driver.get("https://www.fischer.cz/")
root1 = driver.find_element_by_id("usercentrics-root")
shadowroot = driver.execute_script("return arguments[0].shadowRoot", root1)
shadowroot.find_element_by_css_selector("#uc-center-container > div > div.sc-hiKfDv.gGhxCY > div > div.sc-ezzafa.bUHURR > div > button.sc-gtsrHT.ctrzIr").click()

Solution 2:

When the website is launched, I see Přijmout vše button, I think you wanna click on that.

try with below code :-

driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(30)
driver.get("https://fischer.cz/")
wait = WebDriverWait(driver, 20)

def expand_shadow_element(element):
  shadow_root = driver.execute_script('return arguments[0].shadowRoot', element)
  return shadow_root

outer = expand_shadow_element(driver.find_element_by_css_selector("div#usercentrics-root"))
inner = outer.find_element_by_css_selector("button[data-testid='uc-accept-all-button']")
inner.click()

Imports :

from selenium.webdriver.support.uiimportWebDriverWaitfrom selenium.webdriver.common.byimportByfrom selenium.webdriver.supportimport expected_conditions asEC

Post a Comment for "Selenium Accept Cookies Python"