如何使用 python Selenium 关闭动态弹出窗口

How to use Close a dynamic Popup with python Selenium

我想关闭这个弹出窗口,但我不能, 我试过使用 find.element.by.xpath() 我猜有所有不同的可能性, 尝试使用 switch_to _alert().dismiss() 但似乎没有任何帮助 任何帮助深表感谢, 谢谢

似乎不是 警报。它只是网页上的另一个元素。

引入 WebDriverWait() 并等待 element_to_be_clickable() 和跟随 css 选择器

WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"div#dismiss-button"))).click()

您需要导入以下库。

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

注意:如果出现超时错误,请检查元素是否在 iframe 下。如果是这样,您需要切换到 iframe 才能与按钮元素进行交互。