0

I tried to use:

   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 got the error message "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("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, 0 );

        wait.until(ExpectedConditions.alertIsPresent());

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

    alert.accept();






    }   
}

This is my screenshot. Here you can see an alert box. I want to pop up this alert window. I tried several times, but it's not yet working.

screenshot

1 Answer 1

2

That's because switchTo().alert() will only work on JavaScript alerts window.alert("Hello World!"); and the alert in your screenshot is just some HTML styled to look like an alert.

What you want to do instead is locate the alert just like any other HTML element. Something like this:

driver.findElement(By.id("myAlert"));

You can get then get its text. To close the alert you would need to find the OK button and click it. Something like this:

driver.findElement(By.id("okButton")).click();

1
  • This looks very much link my problem, but the driver.findElement(By.id("myAlert")); throw the exception... selenium.common.exceptions.UnexpectedAlertPresentException so can never get to .click() - cf. stackoverflow.com/questions/44568402/…
    – NevilleDNZ
    Jun 16, 2017 at 7:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.