简体   繁体   中英

I am getting error message as “No alert is active” when i run the script using Java in Webdriver

I am getting error message as "No alert is active" when i run the script using Java in Webdriver. This is inconsistent behavior, it gives error sometimes and it does not give error. When I didn't this error message, it will fail at some other location.

Note: The script is working perfectly in my machine.

I have changed all the IE 9 settings in the other machine with my machine setting, from then i am getting this error. previous, i use to get the error message as "Modal Dialog Present".

Please find the code i have used below:

//Entering data in the Account Information section
driver.findElement(By.name(OR.getProperty("AccName_Id"))).clear();
driver.findElement(By.name(OR.getProperty("AccName_Id"))).
sendKeys(AccName+"_"+Randnum);
driver.findElement(By.name(OR.getProperty("AccDBA_ID"))).sendKeys("Kumar_DBA");
driver.findElement(By.id(OR.getProperty("AccTax_ID"))).sendKeys(""+numberToSend);
driver.findElement(By.name(OR.getProperty("AccAddress_ID"))).sendKeys("124 - City  
Cross Roads");
driver.findElement(By.id(OR.getProperty("AccZip_ID"))).sendKeys("00501");
new Actions(driver).sendKeys(driver.findElement(By.id(OR.getProperty("AccZip_ID"))),   
"").perform();

//Entering data in the Main Contact Information section 
driver.findElement(By.name(OR.getProperty("AccFName_ID"))).sendKeys("Kumara");
driver.findElement(By.name(OR.getProperty("AccMName_ID"))).sendKeys("S");
driver.findElement(By.name(OR.getProperty("AccLName_ID"))).sendKeys("Swamy");
driver.findElement(By.id(OR.getProperty("AccOffNo_ID"))).sendKeys("1234567890");
new Actions(driver).sendKeys(driver.findElement(By.id(OR.getProperty("AccOffNo_ID"))), 
"").perform();
driver.findElement(By.id(OR.getProperty("AccCellNo_ID"))).sendKeys("0123456789");
new 
Actions(driver).sendKeys(driver.findElement(By.id(OR.getProperty("AccCellNo_ID"))),  
"").perform();
driver.findElement(By.id(OR.getProperty("AccFaxNo_ID"))).sendKeys("1234567890");
new Actions(driver).sendKeys(driver.findElement(By.id(OR.getProperty("AccFaxNo_ID"))), 
"").perform();
driver.findElement(By.name(OR.getProperty("AccEmail_ID"))).sendKeys("abc@abc.com")     ;
driver.findElement(By.id(OR.getProperty("save_ID"))).click();
driver.switchTo().alert().accept();
isAlertPresent();
Thread.sleep(15000);
driver.findElement(By.id(OR.getProperty("AccZip_ID"))).sendKeys("79081");
new Actions(driver).sendKeys(driver.findElement(By.id(OR.getProperty("AccZip_ID"))), 
"").perform();
Thread.sleep(8000L);
driver.findElement(By.id(OR.getProperty("save_ID"))).click();
driver.switchTo().alert().accept();
isAlertPresent();
Thread.sleep(30000);

Please help me on this issue ASAP. Help will be appreciated deeply.

I think your web driver code fired before the alert renders. My suggest is to put wait for alert after click action.

Try this code:

    //Do some click action to render alert.
    //statement to wait for an alert.
    WebDriverWait wait = new WebDriverWait(driver,
            30);
    wait.until(ExpectedConditions.alertIsPresent());
   //accepting the alert
   driver.switchTo().alert().accept();

The above code will wait for 30 seconds and check the alert presence every 500ms. If the alert is present then it will terminate the wait and do the next action.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM