admin管理员组

文章数量:1245840

I’m trying to accept the cookies on a website, but the pop-up window doesn’t load properly.

Does anyone know how to resolve this?

Website URL: /?access_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjQ4NWIyZWEyLTY2MTItNDU3ZS05YTA4LTRhODBjMTgxNzM4MiJ9.dyAdGEDJ87U-i_lBytN3Y-_kIuJnRKvJKJgVOQ0MlfE

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdrivermon.by import By
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Firefox()
driver.maximize_window()
url = '/?access_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjQ4NWIyZWEyLTY2MTItNDU3ZS05YTA4LTRhODBjMTgxNzM4MiJ9.dyAdGEDJ87U-i_lBytN3Y-_kIuJnRKvJKJgVOQ0MlfE'
driver.get(url)

wait = WebDriverWait(driver, 10)
shadow_host = wait.until(EC.presence_of_element_located((By.XPATH, "//button[contains(text(), 'Alles Akzeptieren')]")))
shadow_root = shadow_host.shadow_root
load = shadow_root.find_element(By.XPATH, "//button[contains(text(), 'Alles Akzeptieren')]")
load.click()

I attempted to print the entire webpage, but the cookie consent pop-up doesn’t appear in the print output.

After inspecting the page, I discovered that the pop-up is inside a shadow root. However, I’m unable to access it.

I’m trying to accept the cookies on a website, but the pop-up window doesn’t load properly.

Does anyone know how to resolve this?

Website URL: https://www.guest.wemolo.at/?access_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjQ4NWIyZWEyLTY2MTItNDU3ZS05YTA4LTRhODBjMTgxNzM4MiJ9.dyAdGEDJ87U-i_lBytN3Y-_kIuJnRKvJKJgVOQ0MlfE

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdrivermon.by import By
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Firefox()
driver.maximize_window()
url = 'https://www.guest.wemolo.at/?access_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjQ4NWIyZWEyLTY2MTItNDU3ZS05YTA4LTRhODBjMTgxNzM4MiJ9.dyAdGEDJ87U-i_lBytN3Y-_kIuJnRKvJKJgVOQ0MlfE'
driver.get(url)

wait = WebDriverWait(driver, 10)
shadow_host = wait.until(EC.presence_of_element_located((By.XPATH, "//button[contains(text(), 'Alles Akzeptieren')]")))
shadow_root = shadow_host.shadow_root
load = shadow_root.find_element(By.XPATH, "//button[contains(text(), 'Alles Akzeptieren')]")
load.click()

I attempted to print the entire webpage, but the cookie consent pop-up doesn’t appear in the print output.

After inspecting the page, I discovered that the pop-up is inside a shadow root. However, I’m unable to access it.

Share Improve this question asked Feb 17 at 16:22 GibbyGibby 111 bronze badge New contributor Gibby is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Add a comment  | 

1 Answer 1

Reset to default 0

The way to do this is to identify the shadow root then locate the relevant button. The specifics of how to do this vary from website to website. In this particular case it's:

from selenium.webdriver import Chrome
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdrivermon.by import By

URL = "https://www.guest.wemolo.at/?access_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjQ4NWIyZWEyLTY2MTItNDU3ZS05YTA4LTRhODBjMTgxNzM4MiJ9.dyAdGEDJ87U-i_lBytN3Y-_kIuJnRKvJKJgVOQ0MlfE"

with Chrome() as driver:
    driver.get(URL)
    wait = WebDriverWait(driver, 10)
    ec = EC.presence_of_element_located
    sel = (By.ID, "usercentrics-root")
    root = wait.until(ec(sel)).shadow_root
    ec = EC.presence_of_element_located
    sel = (By.CSS_SELECTOR, "button[data-testid=uc-accept-all-button]")
    WebDriverWait(root, 10).until(ec(sel)).click()

本文标签: pythonSelenium Unable to Click Button Inside Shadow RootStack Overflow