Handling alerts and frames using selenium C#

Introduction

In this article, we will look at how to handle alerts and frames using selenium with C#.

Pre-requisite

Alerts

The alert() method in JavaScript is used to display a virtual alert box in your application. It is mostly used to give a warning message to the users. It displays an alert dialog box that consists of some specified message (which is optional) and an OK button. When the dialog box pops up, we have to click "OK" to proceed.

The alert dialog box takes the focus and forces the user to read the specified message. So, we should avoid overusing this method because it stops the user from accessing the other parts of the webpage until the box is closed.

We can handle alerts in selenium C# using the below functions.

  • Dismiss 
    • When a user clicks on the button it displays the alert message in the browser. We can use the dismiss method to dismiss the alert.

      This is equal to clicking the cancel button in the alert popup.

  • Accept 
    • When a user clicks on the button it displays the alert message in the browser. We can use the accept method to accept the alert.

      This is equal to clicking the ok button in the alert popup.

  • Text 
    • If the user wants to get the text which is present in the alert pop-up. We can use the text property which will return the text inside the alert popup.
  • SendKeys
    • This function is used to send input to the textbox present on the alert pop-up. This function accepts a string as a parameter.

Code With Explanation comments

namespace seleniumc_;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;

public class Tests
{
     IWebDriver driver;
    [SetUp]
    public void Setup()
    {
         driver = new ChromeDriver();
    }

    [Test]
    public void Test1()
    {
 
            WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
            driver.Url = "application url which u are testing";
 
            IWebElement alertButton = driver.FindElement(By.Id("testing"));
 
            
            alertButton.Click();

            //the SwitchTo() command is used to switch the context to the alert window
 
            var alert_win = driver.SwitchTo().Alert();
         
           //accepting the alert
 
            alert_win.Accept();
           //dismissing the alert
            alert_win.Dismiss();
           //send value to the alert box
            alert_win.SendKeys("tests");
           //getting the text present inside the alert box and printing it out in the console.
            var alerttext=alert_win.Text;
            Console.WriteLine(alerttext);

    }
}

 NoAlertPresentException

if there is no alert box present at the time of switching, then Selenium raises NoAlertPresentException instead.

Handling Frames

Frames, used to divide a web page into multiple sections, require specific handling in Selenium. We can utilize Selenium's methods to navigate through frames seamlessly.

Frames in a webpage refer to a feature that allows web developers to divide a browser window into multiple sections, each of which can display a separate HTML document. Each of these sections, known as frames, acts as an independent HTML document with its own content, allowing developers to create more complex and dynamic web layouts.

Frames are implemented using the <frame> or <iframe> HTML tags.

To switch to the frame, we need to call the switch-to-frame method. We can switch to frame by using frame id or frame name.

driver.SwitchTo().Frame(frame-id);
driver.SwitchTo().Frame("frame-name");

SwitchTo DefaultContent

This command selects either the first frame on the web page or the main document (i.e. < main page>) when the page contains iFrames.

driver.SwitchTo().DefaultContent();

SwitchTo ParentFrame

This command selects the parent frame of the currently selected frame.

driver.SwitchTo().ParentFrame();

NoSuchFrameException

Description: This exception is thrown when the specified frame is not found.

Summary

I hope this article will be helpful in handling Alerts, and frames using selenium with c#

Thanks, Happy learning....!


Similar Articles