簡體   English   中英

如何使用 Selenium Python 單擊接受按鈕

[英]How to click on Accept button using Selenium Python

我想點擊 cookie 接受按鈕,但它不起作用。 有人能幫我嗎?

https://www.forbes.com/consent/?toURL=https://www.forbes.com/billionaires

driver.get('https://www.forbes.com/consent/toURL=https://www.forbes.com/billionaires') 
driver.implicitly_wait(15) buttonAccept =  driver.find_element_by_class_name("call") driver.implicitly_wait(5)
buttonAccept.click()

如果我嘗試通過 xpath 查找元素,我會收到此錯誤:

selenium.common.exceptions.NoSuchElementException:消息:沒有這樣的元素:無法找到元素:{“method”:“xpath”,“selector”:“/html/body/div[8]/div[1]/div/ div[2]/div[2]/a[1]"}

編輯:我認為是 iframe 的問題,但我不知道如何解決。 我想我需要“driver.switch_to_frame”

這不是有效的 url。 但是您可以在您的代碼中嘗試以下操作嗎?

from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.get('https://www.forbes.com/consent/toURL=https://www.forbes.com/billionaires')
WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Accept']"))).click()

cookie 同意管理器彈出窗口中嵌入了iframe ,因此您需要先切換到它,然后才能單擊按鈕。

    iframe_elem = driver.find_element_by_css_selector('iframe[title="TrustArc Cookie Consent Manager"')
    driver.switch_to.frame(iframe_elem)
    accept_btn = driver.find_element_by_css_selector('.call')
    accept_btn.click()

暫無
暫無

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

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