1

I'm trying to accept alert using driver.switchTo().alert() keywords. But its not working for google chrome and Firefox. But its work for IE11. This Error message print in Eclipse Console.This is the alert

This is my code :

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

Are there any solution for this error ?

Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66

3 Answers3

2

You can wait for the alert to appears then accept it.

WebDriverWait wait = new WebDriverWait(driver, 15);
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
alert.accept();
Murthi
  • 5,299
  • 1
  • 10
  • 15
1

why using two times switching to alert?

Alert alert = driver.switchTo().alert(); 
String alertMessage= alert.getText(); 
alert.accept();
System.out.println("Alert msg is : "+alertMessage);
murali selenium
  • 3,847
  • 2
  • 11
  • 20
0

Here is the JavaScript answer. The documentation has examples for all languages. https://www.selenium.dev/documentation/en/webdriver/js_alerts_prompts_and_confirmations/

await driver.wait(until.alertIsPresent()); 
el = driver.switchTo().alert();
await el.accept();
kizziah
  • 69
  • 1
  • 5