如何使用selenium检查警报的可见性

问题描述 投票:-1回答:3

保存数据后会出现一个警报弹出窗口。如何使用Selenium和java检查弹出窗口可见性

java selenium selenium-webdriver
3个回答
1
投票

我建议你使用下面的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; 
    }    
    }   

0
投票
driver.switchTo().alert();

这将帮助您切换到警报弹出窗口,您可以检查可见性。


0
投票

好的,找到弹出窗口的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");
    }
© www.soinside.com 2019 - 2024. All rights reserved.