Selenium C# Tutorial: Handling Alert Windows

Posted by Himanshu Sheth | April 3, 2020
Automation Testing • Selenium C# • Selenium Tutorial •

57234 Views | 7 Min Read

Selenium C-Sharp

This article is a part of our Content Hub. For more in-depth resources, check out our content hub on Selenium C# Tutorial.

Alert windows are pop-up windows that are extensively used across different types of websites or web applications. Alert window alerts the user and expects an action from the user post that alert.

Alerts are also shown when the user is seeking permission to perform a certain operation. Alerts are also used to issue warnings to the end-user of the web product. Alerts are triggered via the alert() method in JavaScript. In this Selenium C# tutorial, we look at using Selenium test automation for handling alert in Selenium C#.

Types Of Alerts In Selenium C#

The different types of alerts in Selenium C# are:

  • Simple Alert – This type of alert in Selenium C# is used for displaying warning or certain information on the screen.
  • Confirmation Alert – This type of alert in Selenium C# seeks permission from the user to continue performing the intended operation. The preferred options could be ‘Yes’ (Continue) or ‘No’ (Cancel).
  • Prompt Alert – This alert in Selenium C# expects the user to enter some information into the text box.

One major difference between alert window and a regular window is that alert window is blocking in nature i.e. the user needs to perform some operation on the alert window before proceeding further on the website (or web application).


Operations On Alerts In Selenium C#

The operation being performed on an alert window depends on the type of alert being used. The common operations on alert windows are:

  • Dismiss – It is used to dismiss (i.e. cancel) the alert.
  • Accept – It is used to accept the alert in Selenium C#.
  • Text – It is used to read the message from the alert window
  • SendKeys – It is used to enter some textual information in the text box provided in the alert window

Shown below is an example of alert window in Selenium C#


For performing valid operations on the alert in Selenium C#, a SwitchTo() operation has to be performed to switch to the alert window. Once the current window is the alert window, appropriate operation i.e. accept, dismiss, etc. can be performed on the alert.

In the further section of the Selenium C# tutorial, we look at Selenium C# examples that use the NUnit test framework for demonstrating usage of alert windows including authentication alerts. In case you haven’t set up visual studio, you can read our article to get a clearer understanding about it.

Handling Different Alert Types Using Selenium C#

This part of the alerts in Selenium C# tutorial caters to handling alert types such as simple, confirmation, and prompt alerts. As the changes in implementation for handling these alerts are not significant hence, we have combined the three in a single source file.

The test URL which contains these different alert types are Commonly used NUnit annotations such as [SetUp], [Test], [Order], and [TearDown] are used in the implementation. In one of the earlier blogs, we covered the NUnit framework in great detail.

There are four test cases that show the handling of alert operations such as accept, dismiss, and SendKeys.

Test Case 1 – test_alert()

An explicit wait of 10 seconds is added to ensure that the alert button (I am a JS Alert) is loaded on the page.


Once the button is loaded, a Click() operation is performed to load the alert window.

Once the alert window is loaded, the SwitchTo() command is used to switch the context to the alert window. The alert is accepted using the accept() method.

The end result which appears in the web element with id=result is compared with expected resultant string.

Test Case 2 – test_confirm()

The alert window pops up on clicking the button ‘Click for JS Confirm’. Like the previous test case, an explicit wait is performed so that the button is loaded.


Once the alert window ‘I am a JS Confirm’ is loaded, accept() method is used to accept the alert operation.

Test Case 3 – test_dimiss()

The alert window used in this test case is the same as the one is test case 2 (test_confirm). The only difference is that instead of the accept() method for alert in Selenium C#, the dismiss method is used to dismiss the alert window.


Once we switch to the alert window, the alert is dismissed using the dismiss method. The resultant text is ‘You clicked: Cancel’.

Test Case 4 – test_sendalert_text()

The button with CssSelector = button[onclick='jsPrompt()'] is located using the Inspect tool in Chrome. Once the button is located, a click operation is performed to trigger the alert.


Once the alert window is opened, the SendKeys() method for alert in Selenium C# is used to populate the alert text box with the text ‘This is a test alert message’. Once the text is entered, accept() method is used to accept the alert message.


Here is the output snapshot from Visual Studio 2019. As shown below, all the four tests have passed.


Handling Authentication Alerts In Selenium C#

Authentication alerts (or pop-ups) are used in websites when the user needs to have valid credentials to access the features of the site. The credentials would normally comprise of a valid user-name and password.

Here is an example of an authentication where you need to enter valid credentials to access the website.


The preferred way of handling authentication alerts in Selenium C# is by passing user-credentials in the URL. After appending user-name and password to the URL, the final URL will be http://[user-name]:[password]

The site used for demonstration is and its credentials are username – admin & password – admin. Hence, the URL on which we perform Selenium test automation is: http://admin:[email protected]/basic_auth

As we have passed valid credentials in the URL, login is successful.

The output message on the screen is compared with the expected message.

Here is the execution snapshot from Visual Studio and Chrome browser


Also read: Using Explicit and Fluent Wait in Selenium


Alert windows are commonly used in websites for warning or information users present on the site. Alerts are normally triggered on the click of a button. Accept, dismiss, and SendKeys are the widely used methods for handling alert in Selenium C#. Selenium test automation for websites that pop-up authentication alerts is also performed using the same fundamentals as normal alert windows. If you like you can also visit our Previous Selenium C# tutorial on setting up visual studio, implicit waits, explicit & fluent wait.

We’ll meet again in the next tutorial for this Selenium C# tutorial series, where I’ll show you how to handle multiple browser windows in Selenium C#. Let’s Automate!

Update: We’ve now completed the Selenium C# tutorial series, so in order to help you easily navigate through the tutorials, we’ve compiled the complete list of tutorials, which you can find in the section below.

Selenium C# Tutorials With Examples

Happy Testing! Do leave your feedback & suggestions in the comments section so that we can co-learn and contribute to jointly solving Selenium test automation problems!


Written by Himanshu Sheth

With 7+ years of experience in test automation, BDD, TDD, Himanshu Sheth work as an automation engineer with LambdaTest. He is also an avid blogger.

Related Articles

Selenium SendKeys

Selenium SendKeys : All You Need To Know

46493 Views | 7 Min Min Read

How to Generate Test Report In NUnit

How To Generate Test Report In NUnit?

78878 Views | 19 Min Min Read

run selenium test using IE driver

How To Run Selenium Tests Using IE Driver?

133627 Views | 18 Min Min Read

Leave a Reply

Your email address will not be published. Required fields are marked *