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
  • 13,480
  • 5
  • 33
  • 61

3 Answers3

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,717
  • 2
  • 10
  • 20
1

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,146
  • 1
  • 9
  • 15
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
  • 71
  • 1
  • 5