Selenium WebDriver - Working with the Alert Boxe element

in this article, I will review the Alert Box element and what are the available options that selenium provides to handle it.


An Alert Box is a unique element that we cannot simply "Inspect" in the HTML code. Therefore all locators that we used until now (e.g CSS, ID, Class, etc) will not be available for use to use.


To handle this technical limitation, Selenium provides a specific namespace and classes that we can use. To do it, just add the "OpenQA.Selenium.Support.UI;" to your project. This will allow us to use the Ialert instance to perform all the operations we need on Alert boxes.


HTML:

<!DOCTYPE html><html><body>	<h1>Demo: alert()</h1>	<script>		alert("This is alert box!");  // display string message			
    </script></body></html>

Alert Box:


class Program

{

static IAlert alert;


static void Main(string[] args)

{

IWebDriver firefoxDriver = new FirefoxDriver();

Backlog.TestPreperationGeneralSites(firefoxDriver, "file:///C:/Users/home-pc3/Downloads/new%201.html");


alert = firefoxDriver.SwitchTo().Alert();


//Print alert text to the console

Console.WriteLine(alert.Text);


//Approve the Alert

alert.Accept();

//Thread.Sleep(3000);

}





36 views0 comments