如何在Selenium中等待警报框执行操作?

时间:2014-04-11 08:34:53

标签: java selenium selenium-webdriver webdriver

我按下取消按钮,而不是根据我的代码检查一些文字。在Chrome和Firefox中,它工作正常,但在IE中需要时间在警报框上执行操作,但代码会移到下一行。

所以我想要一些代码停止,直到操作在警告框中执行,然后进入下一步。我正在使用硒进行自动化测试。

请找一段代码:

Alert al = driver.switchTo().alert();
al.accept();

wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//*[@id='content-body-full']/p[1]")));
assertEquals("Your cancellation request has been successfully executed.",driver.findElement(By.xpath(".//*[@id='content-body-full']/p[1]")).getText().toString());

1 个答案:

答案 0 :(得分:11)

您希望在切换到警报之前等待警报,类似于第3行。 Reference.

编辑:尝试:

new WebDriverWait(driver, 60)
        .ignoring(NoAlertPresentException.class)
        .until(ExpectedConditions.alertIsPresent());

Alert al = driver.switchTo().alert();
al.accept();