Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if an alert exists using WebDriver?

I need to check the existence of Alert in WebDriver.

Sometimes it pops up an alert but sometimes it will not pop up. I need to check if the alert exists first, then I can accept or dismiss it or it will say: no alert found.

like image 509
Yunfei Gu Avatar asked Jul 13 '12 09:07

Yunfei Gu


People also ask

How do you check if there is any alert exists in Selenium?

We can check if an alert exists with Selenium webdriver. An alert is created with the help of Javascript. We shall use the explicit wait concept in synchronization to verify the presence of an alert. Let us consider the below alert and check its presence on the page.

Can Webdriver handle Javascript alert?

WebDriver can get the text from the popup and accept or dismiss these alerts.


2 Answers

public boolean isAlertPresent()  {      try      {          driver.switchTo().alert();          return true;      }   // try      catch (NoAlertPresentException Ex)      {          return false;      }   // catch  }   // isAlertPresent() 

check the link here https://groups.google.com/forum/?fromgroups#!topic/webdriver/1GaSXFK76zY

like image 180
ManMohan Vyas Avatar answered Sep 24 '22 06:09

ManMohan Vyas


The following (C# implementation, but similar in Java) allows you to determine if there is an alert without exceptions and without creating the WebDriverWait object.

boolean isDialogPresent(WebDriver driver) {     IAlert alert = ExpectedConditions.AlertIsPresent().Invoke(driver);     return (alert != null); } 
like image 23
Lee Jensen Avatar answered Sep 23 '22 06:09

Lee Jensen