0

After clicking save button a pop will open and i write a code below to do accept but its not working.

   `driver.findElement(By.id("save")).click();
    Alert succ=driver.switchTo().alert();
    System.out.println(succ.getText());
    Thread.sleep(2000);
    succ.accept();`

enter image description here

Need Help please.

2 Answers2

1

The behavior of selenium varies according to browser, In some case the actions done in Firefox browser like form filling actions or clicking the button is faster then Chrome browser so your script well executed in chrome but throw error in Firefox so you need to add some pause to make them perform well.

So in your case after clicking the save button selenium executing the command too fast so skip to switch on alert and accept so add some wait using Thread.sleep(); to make pause

driver.findElement(By.id("save")).click();
Thread.sleep(2000);
Alert succ=driver.switchTo().alert();
System.out.println(succ.getText());
Thread.sleep(2000);
succ.accept();

Note : It is not recommended to use Thread.sleep(); instead use implicit or explicitwait conditions

WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.alertIsPresent());
NarendraR
  • 7,577
  • 10
  • 44
  • 82
0

Try this:

driver.findElement(By.id("save")).click();
try {
    Thread.sleep(3000);
} catch (InterruptedException e) {
    e.printStackTrace();
}
System.out.println(driver.switchTo().alert().getText());
driver.switchTo().alert().accept();
PrinceOFF
  • 95
  • 3
  • 14
  • 1. At first, we are clicking on needed element. 2. At second, we have to waiting for our element full rendered on our page - for this we are writing `Thread.sleeps(milisec)`. 3. After that we are sure that our element is present on page and we can do with him any manipulations. – PrinceOFF May 12 '17 at 13:29