我正在从网站 (Investing.com) 抓取一些数据,偶尔会弹出一个窗口。我在元素中寻找可点击的按钮,但找不到任何东西。
这是元素页面中的片段,其中 <"i"> 元素是我能找到的与要关闭的“X”相关的所有元素。
...
<div class="right">
<i class="popupCloseIcon largeBannerCloser">
::after
</i>
...
我尝试过的代码如下:
from selenium.webdriver import ChromeOptions as Options
import selenium.webdriver as webdriver
chrome_options = Options()
#chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
d = webdriver.Chrome('chromedriver', options=chrome_options)
d.get("https://www.investing.com/currencies/eur-usd-technical")
iElements = d.find_elements_by_tag_name("i")
for name in iElements:
print(name.get_attribute("class"))
if name.get_attribute("class") == "popupCloseIcon largeBannerCloser":
print("im here")
name.click()
我打印文本并到达那里,所以知道我已经找到了我的元素,但最终出现以下错误:
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
找不到其他可以交互的东西吗?
最佳答案
您确定这不是警报吗? 一种检查方法是尝试检查“警报”窗口中的元素。
您可以尝试以下任一方法:
driver.switchTo().alert().dismiss();
driver.switchTo().alert().accept();
如果抛出 NoAlertPresentException,弹出窗口的屏幕截图可能会帮助我们更好地为您提供帮助。
关于javascript - 使用 Selenium 关闭浏览器弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56825228/