【问题标题】:I can't close a dialog box when I using Selenium and Python使用 Selenium 和 Python 时无法关闭对话框
【发布时间】:2021-11-09 06:11:12
【问题描述】:

当弹出消息窗口时,我正在使用 Selenium 收集数据,这个窗口只是一个手册,所以我想将其关闭。我尝试了很多方法,例如:find element by idxpath、......它不起作用,甚至切换到警报也不起作用。下面是网站的html截图。

感谢您的所有指导。

enter image description here

【问题讨论】:

  • 嗨!如果数据在该对话框下方,您就不能忽略它吗?还是需要关闭它才能加载下面的内容?
  • 到目前为止您尝试过什么。你得到什么错误?
  • 您好,我必须关闭对话框,我可以下载数据,但我不知道如何关闭此对话框。我只是自动按 x 。按钮
  • 请提供您的 python sn-p 和错误日志以便更清楚,我们可以根据这些尝试提供帮助
  • @SonLe : 网址是公开的吗?

标签: python selenium selenium-webdriver automation


【解决方案1】:

根据屏幕截图,span 标签有classclose-btn。我猜点击Element 会关闭弹出窗口。尝试以下几行。如果弹出窗口需要时间加载,还应用了Explicit Waits

#imports required:
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

wait = WebDriverWait(driver,30)
#Trial 1:
#driver.find_element_by_xpath("//div[@class='modal=dialog']//span[conatins(@class,'close-btn')]").click()
wait.until(EC.element_to_be_clickable((By.XPATH,"//div[@class='modal=dialog']//span[conatins(@class,'close-btn')]"))).click()
# Can also use `ends-with` instead of `contains` in the xpath.

#Trial -2
#driver.find_element_by_xpath("//div[@role='dialog']//span(contains(@class,'close-btn'))").click()
wait.until(EC.element_to_be_clickable((By.XPATH,"//div[@role='dialog']//span(contains(@class,'close-btn'))"))).click()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-08
    • 2011-09-18
    相关资源
    最近更新 更多