selenium-webdriver Handle an alert C#

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

Here's how to close a popup alert in C# with Selenium:

IAlert alert = driver.SwitchTo().Alert(); 
// Prints text and closes alert
System.out.println(alert.Text);
alert.Accept();
or
alert.Dismiss();

according to your needs.

Another way you can do this, is wrap your code inside a try-catch:

try{
   // Your logic here.
} catch(UnhandledAlertException e){
  var alert = driver.SwitchTo().Alert();
  alert.Accept();
}
// Continue.


Got any selenium-webdriver Question?