Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How wait for alert box to perform the action in Selenium?

I am pressing a cancel button than according to my code it is checking some text. In Chrome and Firefox it is working fine but in IE it is taking time to perform operation on alert box but code moves to next line.

So i want some code that stop till the operation is preformed on alert box and than it goes to next Step. I am doing automation testing using selenium.

Please find piece of code:

Alert al = driver.switchTo().alert();
al.accept();

wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//*[@id='content-body-full']/p[1]")));
assertEquals("Your cancellation request has been successfully executed.",driver.findElement(By.xpath(".//*[@id='content-body-full']/p[1]")).getText().toString());
like image 637
OPTIMUS Avatar asked Apr 11 '14 08:04

OPTIMUS


People also ask

How do you use explicit wait for alerts?

We use Explicit wait as WebDriverWait wait = new WebDriverWait(driver,10) ; and wait. until(ExpectedCondition. alertIsPresent()); If the alert is present, then we will accept it using driver.

How do you wait for an object in Selenium?

Explicit Wait in SeleniumBy using the Explicit Wait command, the WebDriver is directed to wait until a certain condition occurs before proceeding with executing the code. Setting Explicit Wait is important in cases where there are certain elements that naturally take more time to load.

What are the wait commands in Selenium?

The Implicit Wait in Selenium is used to tell the web driver to wait for a certain amount of time before it throws a “No Such Element Exception”. The default setting is 0. Once we set the time, the web driver will wait for the element for that time before throwing an exception.


1 Answers

You want to wait until the alert is present before switching to it, similar to your 3rd row. Reference.

EDIT: try:

new WebDriverWait(driver, 60)
        .ignoring(NoAlertPresentException.class)
        .until(ExpectedConditions.alertIsPresent());

Alert al = driver.switchTo().alert();
al.accept();
like image 72
Erki M. Avatar answered Sep 21 '22 10:09

Erki M.