3

When recording in selenium IDE I can click the "OK" button in a popup, and expected to be able to click it using

driver.findElement(By.linkText("OK")).click();

but this was not the case.

Similarly this doesn't work.

driver.switchTo().alert().accept();

Selenium throws a NoAlertPresent exception. If what's popping up is not an alert, then what is it? And how do I click yes!

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
confusified
  • 2,320
  • 7
  • 22
  • 29
  • Is it an actual Javascript confirmation? If it isn't, then the `IAlert` interface you are calling by doing `.alert()` won't help you. – Arran Oct 15 '12 at 11:39
  • As it's not Alert no need the line of code: driver.switchTo().alert().accept(); – Ripon Al Wasim Mar 23 '15 at 12:51

3 Answers3

2

It could be anything. You should be telling us that.

If it is a Java Script alert then, this should work

driver.switchTo().alert().accept();

At the very least you could try sending enter/return key stroke, if the "OK" button is autoselected/highlighted by the web app.

import org.openqa.selenium.Keys
WebElement.sendKeys(Keys.RETURN);

Update


It could also be because your alert is not present at the time you are trying to click/accept it. For a quick check put in a sleep of 4-5 seconds and then try driver.switchTo().alert().accept();. Once it is ascertained, then put in a wait for alert present in a try and catch loop (any exception handling).

Amey
  • 8,470
  • 9
  • 44
  • 63
  • I'm new to the whole automation deal. However I have also tried your idea of Keys.return to no avail. All I know for certain is that the line driver.switchto().alert.accept() has worked for me on other applications. This is why I'm confused that it's not working here. – confusified Oct 15 '12 at 10:27
  • do you have firebug(firefox add on) installed? if no download it. Using that addon, right click on that "ok" button of the popup/alert and select inspect with firebug. – Amey Oct 15 '12 at 10:41
2

in such case I'd prefer to check(verify) the alert presence on the page and then if is present - accept it. It be somthing like:

public boolean isAlertPresent() {

  boolean presentFlag = false;

  try {

   // Check the presence of alert
   Alert alert = driver.switchTo().alert();
   // Alert present; set the flag
   presentFlag = true;
   // if present consume the alert
   alert.accept();

  } catch (NoAlertPresentException ex) {
   // Alert not present
   ex.printStackTrace();
  }

  return presentFlag;

 }

here you can get details Also do not forget about debug step by step.

Hope this helps you.

eugene.polschikov
  • 7,254
  • 2
  • 31
  • 44
1

if you are using latest version of webdriver, infact anything above 2.20 then

driver.switchTo().alert().accept();

should work provided the alert is a javascript alert similar to the one we get when we click

alert demo OR confirm pop-up demo

Updated

here this code will help you accept the alert

driver = new FirefoxDriver();
String baseUrl = "http://www.w3schools.com/js/tryit.asp?filename=tryjs_alert";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get(baseUrl);
driver.switchTo().frame(0);
driver.findElement(By.cssSelector("input[type=\"button\"]")).click();
driver.switchTo().alert().accept();
Prashant Shukla
  • 1,391
  • 9
  • 18
  • I don't understand. I would of thought that performing the action is what generates the alert, so how then could I switch to the alert before performing the action? – confusified Oct 15 '12 at 10:30
  • Sorry I typed wrong alert should be accepted just after the action. Updated the answer. – Prashant Shukla Oct 15 '12 at 13:18