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

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



德玛西亚99
浏览 121回答 3
3回答

慕工程0101907

我建议您使用如下所示的 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;     }        }   

大话西游666

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");    }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java