3 回答

TA贡献1887条经验 获得超5个赞
我建议您使用如下所示的 ExpectedConditions 来检查警报是否存在,如下所示:
WebDriverWait wait = new WebDriverWait(driver, 30);
if(wait.until(ExpectedConditions.alertIsPresent())==null)
{
System.out.println("No alert");
else{
System.out.println("Alert present");
}
}
要接受该警报,您可以使用:
driver.switchTo().alert().accept();
要关闭该警报,您可以使用:
driver.switchTo().alert().dismiss();
要将一些值发送到警报框,您可以使用:
driver.switchTo().alert().sendKeys("Text");
我们也可以使用如下的 try-catch 块:
public boolean alertPresent()
{
try
{
driver.switchTo().alert();
return true;
}
catch (NoAlertPresentException Ex)
{
return false;
}
}

TA贡献1906条经验 获得超10个赞
好的,找到弹出窗口的xpath或id让我们举个例子
WebElement popUp = driver.findElement(By.xpath("xpath of the popup"));
if (popUp.isDisplayed())
{
WebElement btnOk = driver.findElement(By.xpath("xpath of the button in alert"));
btnOk.click();
System.out.println("Pop up displayed and button clicked");
}
else
{
System.out.println("Pop up Not found");
}
他们是您处理警报并检查是否存在的第二种方式
try
{
Alert alert = driver.switchTo().alert();
System.out.println(alert.getText()+" Alert is Displayed");
}
catch(NoAlertPresentException ex)
{
System.out.println("Alert is NOT Displayed");
}
添加回答
举报