python selenium检测警报错误

时间:2016-03-28 16:26:01

标签: python selenium

这个page当数字错误数据打开警报错误时,我需要检查此警报是否打开,如果打开需要关闭,我写这个代码蝙蝠不工作:

示例提醒图片:http://snag.gy/8WM0q.jpg

我的实际代码:

driver = webdriver.Firefox()
driver.get("https://secure.ingdirect.it/login.aspx")
driver.find_element_by_id("ctl00_cphContenuto_LoginContainerUC1_LoginStepCifUC1_txtCodiceCliente").clear()
driver.find_element_by_id("ctl00_cphContenuto_LoginContainerUC1_LoginStepCifUC1_txtCodiceCliente").send_keys('1234567')
driver.find_element_by_id("ctl00_cphContenuto_LoginContainerUC1_LoginStepCifUC1_txtgg").clear()
driver.find_element_by_id("ctl00_cphContenuto_LoginContainerUC1_LoginStepCifUC1_txtgg").send_keys("01")
driver.find_element_by_id("ctl00_cphContenuto_LoginContainerUC1_LoginStepCifUC1_txtmm").clear()
driver.find_element_by_id("ctl00_cphContenuto_LoginContainerUC1_LoginStepCifUC1_txtmm").send_keys("01")
driver.find_element_by_id("ctl00_cphContenuto_LoginContainerUC1_LoginStepCifUC1_txtaaaa").clear()
driver.find_element_by_id("ctl00_cphContenuto_LoginContainerUC1_LoginStepCifUC1_txtaaaa").send_keys("1999")
driver.find_element_by_id("ctl00_cphContenuto_LoginContainerUC1_LoginStepCifUC1_lnbvanti").click()

if self.is_element_present(By.LINK_TEXT, "chiudi"):
    driver.find_element_by_link_text("chiudi").click()
    return

我如何检查此警报是否存在,并将其关闭?

1 个答案:

答案 0 :(得分:0)

您只需使用以下代码检查是否显示弹出窗口(如果打开则关闭弹出窗口或如果没有弹出窗口则不执行任何操作):

from selenium.common.exceptions import NoSuchElementException
try:
    driver.find_element_by_xpath('//a[@class="popuptipo1chiudi close"]').click()
except NoSuchElementException:
    pass