Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

org.openqa.selenium.NoAlertPresentException: no alert open

My test checks to see if I can send a message to the mail. When I click the send button should check the alert.

My code for alert is:

        Alert alert = driver.switchTo().alert();
        System.out.println(alert.getText());
        alert.accept();
        Assert.assertTrue(alert.getText().contains("Thanks."));

My error is:

Starting ChromeDriver 2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed) on port 13707
Only local connections are allowed.

org.openqa.selenium.NoAlertPresentException: no alert open
  (Session info: chrome=55.0.2883.87)
  (Driver info: chromedriver=2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed),platform=Windows NT 6.3.9600 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 76 milliseconds
Build info: version: 'unknown', revision: '31c43c8', time: '2016-08-02 21:57:56 -0700'
System info: host: 'Gaga', ip: '192.168.1.6', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.8.0_101'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed), userDataDir=C:\Users\Dragana\AppData\Local\Temp\scoped_dir6652_7181}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=55.0.2883.87, platform=WIN8_1, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: b7131fce27b2ef40f1daff3c82188e7c
like image 285
MY DZON Avatar asked Dec 24 '22 22:12

MY DZON


1 Answers

The code has to wait for alert. Below is the sample code

try {
    WebDriverWait wait = new WebDriverWait(driver, 2);
    wait.until(ExpectedConditions.alertIsPresent());
    Alert alert = driver.switchTo().alert();
    System.out.println(alert.getText());
    alert.accept();
    Assert.assertTrue(alert.getText().contains("Thanks."));
} catch (Exception e) {
    //exception handling
}
like image 158
Shambu Avatar answered Feb 16 '23 09:02

Shambu