0

I have a scenario in which clicking on "Save" button can give three different alerts based on the data entered. It can give "Saved successfully", "User already registered" and "Username already exist" alerts. I have tried:

driver.findElement(By.id("dnn_ctr5995_View_btnsavesession")).click();
    Thread.sleep(10000);
Alert alert = driver.switchTo().alert();
    String A = alert.getText();
    if (A == "Username already exist"){
        System.out.println("Admission number already exist.");
        alert.accept();
    } else if (A == "User already registered"){
        System.out.println("User already registered");
        alert.accept();
    } else if (A == "Saved Successfully."){
        System.out.println("Saved Successfully.");
        alert.accept();
    }

But it is showing No Alert present exception. I have tried increasing the sleep time but still it shows the same exception.

Dinu
  • 845
  • 3
  • 13
  • 27

3 Answers3

0

Looks like your clicking is not actually clicking because the 'save' id is no like in the element. Try to do it like this:

    driver.findElement(By.cssSelector("[id*=save"])).click();

And then trying to catch the alert. Please note the '*' in selector.

Johnny
  • 14,397
  • 15
  • 77
  • 118
  • this is an incorrect CSS selector `:)` It needs to be wrapped in `[]` if you are trying to identify an attribute. e.g.: `[id*='save']` – ddavison Jul 21 '14 at 15:20
  • sure, sorry for the typo. fixing the mistake :) – Johnny Jul 21 '14 at 15:35
  • I have used the same id as mentioned in the HTML tag but still it is showing the same error. – Dinu Jul 22 '14 at 04:55
  • Can you please elaborate in what phase you experience the error - is the clicking working and you just can catch the alert or the clicking is not working to? – Johnny Jul 22 '14 at 09:08
0

As per the Html shared, the id of the element is not "save". So the first line of the shared code should be modified a bit to achieve the output.

Change the element identification tag to :

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

or if the 'id' of the element is changing on every instance, then it can be changed to:

driver.findElement(By.xpath("//*[contains(@id,'_View_btnsavesession')]")).click();
Praveen
  • 1,387
  • 1
  • 12
  • 22
  • I have used the same id as mentioned in the HTML tag but still it is showing the same error. – Dinu Jul 22 '14 at 04:56
0

I'm guessing you never actually call alert.accept() because you're using A == "User already registered" instead of "User already registered".equalsIgnoreCase(A). If you change the if conditions, that should fix your problem.

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428