Selenium with C#

WebElements Commands in Selenium WebDriver with C Sharp

WebElements Commands in Selenium WebDriver with C Sharp

When you start working with Selenium with C# then you will be working with many Interfaces and Classes. In this post I will guide you about different WebElements Commands in Selenium WebDriver with C Sharp.

In our previous post we already discussed about Browser Navigation Commands which can only handle Browser related event but when you have to work with WebElement then you have to use IWebElement Interface which is one of the most used Interface throughout your Selenium Journey with C#.

IWebElement is a Interface in Selenium C# which inherit ISearchContext interface. IWebElement has many method which made our task easy. Lets discuss them one of one.

WebElements Commands in Selenium WebDriver with C Sharp

WebElements Commands in Selenium WebDriver with C Sharp

Commands

Description Syntax
Click() This method is used to click on webelement. void IWebElement.Click()
SendKeys(String value) This method is used to send values to webelement. void IWebElement.SendKeys(String value)

where value is the value to be entered in webelement.

Close() This method closes the browser window that the driver has focus of. void IWebDriver.Close()
Dispose() This method closes all browser windows and safely ends the session void IWebDriver.Dispose()
Quit() This method internally calls Dispose() method void IWebDriver.Quit()
FindElement() This method is used to Find first WebElement in webpage using some properties like Xpath, ClassName, CssSelector, Id, etc..  IWebElement ISearchContext.FindElement(By.Xpath(xpathexpression))

Return type of FindElement method is IWebElement.

FindElements() This method is used to Find WebElements in webpage using some properties like Xpath, ClassName, CssSelector, Id, etc.. <IWebElement> ISearchContext.FindElements(By.Xpath(xpathexpression))

Return type of FindElements method is array of WebElements.

SwitchTo() This method is used to switch to Frames, Windows, Alert, ActiveElement ITargetLocator IWebDriver.SwitchTo()

return type is the target locator.

Manage() This method is used to perform windows based operations like maximize, minimize etc..  IOptions IWebDriver.Manage()

return type is options

 

 

Submit() This method will submit the form and will send to server. I personally don’t use this. driver.FindElement(By.Id(“”)).Submit();
GetAttribute() This method will fetch attribute from WebElement and will return as String String

data=driver.FindElement(By.Id(“”)).GetAttribute(“Attribute Name”);

GetCssValue() This method will return css attribute of WebElement which you will pass and will return String value String data=driver.FindElement(By.Id(“”)).GetCssValue(“CSS Attribute”);
Properties Description Syntax
Text Capture the text from WebElement and return String String data=driver.FindElement(By.Id(“”)).Text;
Selected This will get whether element is Selected or not and this will be only applicable for input type like Radio button, Checkbox etc Boolean data=driver.FindElement(By.Id(“”)).Selected;
Enabled This will check whether element is enabled or not and return Boolean Boolean data=driver.FindElement(By.Id(“”)).Enabled;
Displayed This will check of element presence in DOM and will return Boolean Boolean data=driver.FindElement(By.Id(“”)).Displayed;
TagName It finds the TagName of element and return as a String String data=driver.FindElement(By.Id(“”)).TagName;
Size It returns the Size of WebElement like Widht and Height int height=driver.FindElement(By.Id(“”)).Size.Height;
int width = driver.FindElement(By.Id(“”)).Size.Width;
Location It help us to find coordinates  of WebElement int x = driver.FindElement(By.Id(“”)).Location.X;
int y = driver.FindElement(By.Id(“”)).Location.Y;

 

Usage of Browser Commands with example

Note: SwitchTo command is explained in detail in further blog.

Let’s write a program to login to gmail and sign out of gmail.

Code explanation.

//declare a driver instance
IWebDriver driver;

//Initialize the instance
driver = new FirefoxDriver();

//launch gmail.com
driver.Navigate().GoToUrl("http://gmail.com");

//maximize the browser
driver.Manage().Window.Maximize();

//find the element by xpath and enter the email address which you want to login.
driver.FindElement(By.XPath("//input[@aria-label='Email or phone']")).SendKeys(email adress);

//wait for a seconds
Task.Delay(1000).Wait();

//find the Next Button and click on it.
driver.FindElement(By.XPath("//*[text()='Next']/../..")).Click();


//wait for 2 seconds
Task.Delay(2000).Wait();


//find the password field and enter the password for the gmail.
driver.FindElement(By.XPath("//input[@type='password']")).SendKeys(password value);


//find the Next Button and click on it.
driver.FindElement(By.XPath("//*[text()='Next']/../..")).Click();

//get the hash code.
int hashCodeValue = driver.GetHashCode();

//print the value.
Console.WriteLine("Value is "+hashCodeValue);

//find the element by xpath n click.
driver.FindElement(By.XPath("(//a[@role='button'])[3]")).Click();

//click on sign out.
driver.FindElement(By.XPath("//a[text()='Sign out']")).Click();

//wait for a second
Task.Delay(1000).Wait();

//close the driver instance.
driver.Close();

//quit
driver.Quit();

 

 

WebElements Commands in Selenium WebDriver with C Sharp

WebElement Commands in Selenium WebDriver

 

All commands which we discussed above are very important and we use the same almost in every script that we create. I hope you enjoyed this article and if yes then please share with your friends.

I also wanted to share one pro tip which is link which contains all classes and interface links which will help you in order to excel in Selenium with C#.

Complete Documentation of Selenium WebDriver C# Classes and Interface

 

author-avatar

About Mukesh Otwani

I am Mukesh Otwani working professional in a beautiful city Bangalore India. I completed by BE from RGPV university Bhopal. I have passion towards automation testing since couple of years I started with Selenium then I got chance to work with other tools like Maven, Ant, Git, GitHub, Jenkins, Sikuli, Selenium Builder etc.

16 thoughts on “WebElements Commands in Selenium WebDriver with C Sharp

  1. andersonmwati says:

    how can do to Find a Hidden element? i really need help

    1. Hi Andersonmwati,

      You can use JavascriptExecutor to enable elements forcibly to interact as per your requirement.

  2. Praveen Kumar says:

    Hi mukesh

    Thank you for providing the selenium by c# it’s amazing to learn
    Actually I have experience in selenium by java now I want to learn selenium by c# but I want how to create frame works in c# like that in java by using maven ,testng how canwe build in c#
    Could you please give me overview about that topics in c#

    Thanks in advance

    1. Hi Praveen, you can checkout executeAutomation channel on youtube. He has made many videos on C# with Selenium.

  3. Vivek Shah says:

    Hi Mukesh,

    Thank you very much for your all the articles. I have been following you since long time. Now this C# articles helped me in my current project.

    Could you please post an article regarding fetching the data from excel using C# as well like you did for java.?

    Thank you in advance.

    1. Hi Vivek,

      I’ll post it soon…

  4. Monalisha says:

    Hi Mukesh, I was searching for Selenium using c# using from quite a long time. Thank you so much for this. Do you have youtube channel for this series as well ?

    1. Hi Monalisha,

      This is my only youtube channel playlist https://www.youtube.com/c/Mukeshotwani/playlists
      WRT Selenium with C#, I have lesser videos but more are in pipeline. PLease stay tuned…:)

      1. Rehna says:

        Hi Mukesh ,
        Is it already available, the selenium with C#
        waiting for those complete tutorial

        1. Hi Rehna,

          Right now, I don’t have full course of Selenium with C# but I’ve plans for same. Stay tuned…:)

  5. lavanya says:

    How to handle drop down if particular drop down is not a select tag in HTML

    1. Hi Lavanya,

      Selenium provides support for select tag only. If it is not available then work around has to be done like first click on control then wait for some time following by check of properties available in DOM. You can refer this link for reference https://vistasadprojects.com/mukeshotwani-blogs-v2/handle-bootstrap-dropdown-in-selenium-webdriver/

  6. Hitekschools says:

    Thank you for this post, it is a very useful reference

  7. Manikanta says:

    Hi Mukesh,

    How can we read numerical data from Excelsheet

    1. Hi Manikanta,

      Use cell.getNumericCellValue() method.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.