Antony Naveen Antony Naveen - 1 month ago 13
Java Question

Why my alert is not popping up?

I tried to put this code.

driver.findElement(By.xpath("//*[@id='adduser']/div/form/div[2]/div/div/label")).click();
Thread.sleep(1000);

Alert alert=driver.switchTo().alert();
String alertMessage=driver.switchTo().alert().getText();
System.out.println(alertMessage);
alert.accept();


I found the error message like "no alert is present".
What should i do?
complete java code is below..

package AddUser;



import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

public class CheckFirstname
{
public static WebDriver d;
public static void main(String []args)throws Exception{
System.setProperty("webdriver.firefox.marionette","pathToGeckodriver");
FirefoxDriver driver=new FirefoxDriver();


driver.get("http://54.68.159.204/qmsadm");
driver.manage().window().maximize();
WebElement element=driver.findElement(By.name("username"));
element.sendKeys("user name");
element=driver.findElement(By.name("password"));
element.sendKeys("password");
element.submit();
element=driver.findElement(By.linkText("Admin"));
element.click();
element=driver.findElement(By.linkText("Users"));
element.click();
element=driver.findElement(By.xpath("//*[@id='search-container']/a/i"));
element.click();




element= driver.findElement(By.id("username"));
element.sendKeys("#!#!#$@#!$@!$@#$%#%^#$^^&%&$%*");

driver.findElement(By.xpath("//*[@id='adduser']/div/form/div[2]/div/div/label")).click();
Thread.sleep(1000);

WebDriverWait wait = new WebDriverWait(driver, 0 );

wait.until(ExpectedConditions.alertIsPresent());

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

alert.accept();






}
}


check this screen shot
This is my screenshot. here you can see an alert box. I want to pop up this alert window. i tries several times, but it's not yet working.

Answer

Do something like this:

Before you try to switch to the alert, it needs to be present.

driver.findElement(By.xpath("//*[@id='adduser']/div/form/div[2]/div/div/label")).click(); Thread.sleep(1000);
wait.until(ExpectedConditions.alertIsPresent());

Alert alert = driver.switchTo().alert();
alertText = alert.getText();
alert.accept();

and why you are getting alert here Alert alert=driver.switchTo().alert(); if you are not using in the below line String alertMessage=driver.switchTo().alert().getText();, you are just writing it unnecessary. Hope it helps. complete java code is below.

package AddUser;



import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

    public class CheckFirstname 
    {
    public static WebDriver d;
        public static void main(String []args)throws Exception{
           System.setProperty("webdriver.firefox.marionette","pathToGeckodriver");
           FirefoxDriver driver=new FirefoxDriver();


           driver.get("http://54.68.159.204/qmsadm");
           driver.manage().window().maximize();
           WebElement element=driver.findElement(By.name("username"));
           element.sendKeys("geosony");
           element=driver.findElement(By.name("password"));
           element.sendKeys("1");
           element.submit();
           element=driver.findElement(By.linkText("Admin"));
           element.click();
           element=driver.findElement(By.linkText("Users"));
           element.click();
           element=driver.findElement(By.xpath("//*[@id='search-container']/a/i"));
           element.click();




           element= driver.findElement(By.id("username"));
           element.sendKeys("#!#!#$@#!$@!$@#$%#%^#$^^&%&$%*");

           driver.findElement(By.xpath("//*[@id='adduser']/div/form/div[2]/div/div/label")).click();
           Thread.sleep(1000);

        WebDriverWait wait = new WebDriverWait(driver, 30 );

            wait.until(ExpectedConditions.alertIsPresent());

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

        alert.accept();






        }   
    }