Handling alerts in selenium

Alerts don’t have any xpath in selenium.We can switch to an alert using the below command.

Alert alert = driver.switchTo().alert(); \\We make an Alert interface
System.out.println(alert.getText());\\Then we can get the text present on the alert

\\Then we can either click ok on alert or click it using the below commands

alert.accept();
alert.dismiss)();

driver.switchTo().defaultContent(); \\After that we switch control to main window

//But sometimes alert takes a while to load and the below command is executed before that
Alert alert  = driver.switchTo().alert();

//So to avoid any errors we use explicit wait commands.

WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.alertIsPresent());