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.1 Answer
Reset to default 0The 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
版权声明:本文标题:python - Selenium: Unable to Click Button Inside Shadow Root - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740141881a2231047.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论