te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>python - Selenium: Unable to Click Button Inside Shadow Root - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Selenium: Unable to Click Button Inside Shadow Root - Stack Overflow

programmeradmin5浏览0评论

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()
发布评论

评论列表(0)

  1. 暂无评论