[英]How check visibility of alerts using selenium
保存数据后,将出现一个警报弹出窗口。 如何在Java中使用Selenium检查弹出窗口的可见性
我建议您使用如下所示的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;
}
}
driver.switchTo().alert();
这将帮助您切换到警报弹出窗口,并且可以检查可见性。
好吧,找到弹出窗口的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");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.