簡體   English   中英

無法點擊“接受”cookies,如何找到正確的框架?

[英]Can't click on 'Accept' cookies, how to find correct frame?

我正在嘗試在 cookie 彈出窗口中單擊“Alles accepteren”(英文:Accept all),但似乎沒有任何效果。 這可能與關注正確的 window 有關。 任何幫助,將不勝感激。

我的代碼:

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

driver = webdriver.Chrome(executable_path="path to chromedriver.exe")
driver.maximize_window()

driver.get("https://www.hornbach.nl/")

wait = WebDriverWait(driver,30)

wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@title='??']")))

cookie = wait.until(EC.element_to_be_clickable((By.XPATH,"//button[text()='Alles accepteren']")))

這是在該網站上接受 cookies 的一種方式:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import pandas as pd

import time as t

chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument('disable-notifications')

chrome_options.add_argument("window-size=1280,720")

webdriver_service = Service("chromedriver/chromedriver") ## path to where you saved chromedriver binary
browser = webdriver.Chrome(service=webdriver_service, options=chrome_options)
wait = WebDriverWait(browser, 20)
url = 'https://www.hornbach.nl/'

browser.get(url) 
parent_el = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, 'div[id="usercentrics-root"]')))
parent_el_shadow_root = parent_el.shadow_root 
t.sleep(5)
accept_all_button = parent_el_shadow_root.find_element(By.CSS_SELECTOR, 'button[data-testid="uc-accept-all-button"]')
accept_all_button.click()
print('accepted cookies')

請記住,這種定位影子根的方式僅適用於 Chrome/chromedriver,如果使用 Firefox 或其他瀏覽器/驅動程序,則需要其他方法。 Selenium 文檔位於https://www.selenium.dev/documentation/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM