try {
writeToFile(webdriver.getPageSource(), pageSource);
} catch (UnhandledAlertException e) {
if (retryIfAlert) {
try {
Alert alert = webdriver.switchTo().alert();
System.err.println(e + ": " + alert.getText());
alert.accept();
savePageSourceToFile(fileName, webdriver, false);
}
catch (Exception unableToCloseAlert) {
System.err.println("Failed to close alert: " + unableToCloseAlert);
}