![](/img/trans.png)
[英]How to identify and click on the Accept All button using Selenium
[英]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.