• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Programming Languages
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
  • Automation Tools and Different Tools
    • Web Automation
      • Selenium with Java
        • Selenium Basic
        • Selenium Advance
        • Selenium Realtime
        • Framework
        • Selenium Interview
        • Selenium Videos
        • Selenium with Docker
      • Selenium with Python
      • WebdriverIO
        • Selenium Webdriver C# Tutorial
      • Cypress
      • Playwright
    • TestNG
    • Cucumber
    • Mobile Automation
      • Appium
    • API Testing
      • Postman
      • Rest Assured
      • SOAPUI
    • testRigor
    • Katalon
    • TestProject
    • Serenity BDD
    • Gradle- Build Tool
    • RPA-UiPath
    • Protractor
    • Windows Automation
  • Automation For Manual Testers
  • Services
  • Online Training
  • Contact us
  • About me
  • Follow us
    • Linkedin
    • Facebook Group
    • Facebook Page
    • Instagram

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / Selenium with C# / WebElements Commands in Selenium WebDriver with C Sharp

WebElements Commands in Selenium WebDriver with C Sharp

August 26, 2018 by Mukesh Otwani 16 Comments

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

 

Filed Under: Selenium with C# Tagged With: WebElements Commands in Selenium WebDriver with C Sharp

Reader Interactions

Comments

  1. andersonmwati says

    March 4, 2021 at 4:06 AM

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

    Reply
    • Mukesh Otwani says

      March 4, 2021 at 5:49 PM

      Hi Andersonmwati,

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

      Reply
  2. Praveen Kumar says

    March 6, 2020 at 11:48 AM

    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

    Reply
    • Mukesh Otwani says

      March 10, 2020 at 2:49 PM

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

      Reply
  3. Vivek Shah says

    March 4, 2020 at 4:43 PM

    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.

    Reply
    • Mukesh Otwani says

      March 4, 2020 at 5:26 PM

      Hi Vivek,

      I’ll post it soon…

      Reply
  4. Monalisha says

    April 2, 2019 at 2:49 PM

    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 ?

    Reply
    • Mukesh Otwani says

      April 2, 2019 at 9:28 PM

      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…:)

      Reply
      • Rehna says

        July 10, 2020 at 6:49 PM

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

        Reply
        • Mukesh Otwani says

          July 11, 2020 at 9:31 PM

          Hi Rehna,

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

          Reply
  5. lavanya says

    November 5, 2018 at 12:06 PM

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

    Reply
    • Mukesh Otwani says

      November 6, 2018 at 12:35 PM

      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 http://learn-automation.com/handle-bootstrap-dropdown-in-selenium-webdriver/

      Reply
  6. Hitekschools says

    September 20, 2018 at 12:04 PM

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

    Reply
    • Mukesh Otwani says

      September 20, 2018 at 5:21 PM

      Thanks…:)

      Reply
  7. Manikanta says

    September 12, 2018 at 9:30 PM

    Hi Mukesh,

    How can we read numerical data from Excelsheet

    Reply
    • Mukesh Otwani says

      September 14, 2018 at 10:08 AM

      Hi Manikanta,

      Use cell.getNumericCellValue() method.

      Reply

Leave a Reply Cancel 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.

Primary Sidebar

Free Selenium Videos

https://www.youtube.com/watch?v=w_iPCT1ETO4

Search topic

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?

Stay connected via Facebook

Stay connected via Facebook

Archives

Footer

Categories

Recent Post

  • API Testing Using Postman And RestAssured
  • Disable Personalise Your Web Experience Microsoft Edge Prompt In Selenium
  • How To Fix Error: No tests found In Playwright
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Best and easy way to Group test cases in selenium

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?