Selenium C# Tutorial: Using the Text Field and Button elements (C#)
Updated: Mar 5
In this article, I will review the most commonly used elements: the text box (A web element that enables the user to work with text.) and the button (a controller that allows the user to trigger a new event).
The available methods and properties for working with these elements:

Note:
When using text fields, the “.Text” property will not work as in text boxes; the text resides under the ”value” attribute. So to use it, you will have to use the “GetAttribute("value")” method.
Algorithm:
1. Log in to the machtested blog.
2. Enter Text to the search field and remove it (SendKeys () and Clear ()).
3. Re-enter text and print it to the console.
4. Click the “Search” button.

Code:
class Program
{
static void Main(string[] args)
{
IWebDriver firefoxDriver = new FirefoxDriver();
string searchField = "q";
string searchButton = "gsc-search-button";
Backlog.TestPreperationForMachTested(firefoxDriver);
Thread.Sleep(3000);
Backlog.LocateSearchFieldInBlog(firefoxDriver, searchField, "Text Example");
Backlog.LocateButton(firefoxDriver,searchButton);
}
}
static public class Backlog
{
static IWebElement searchFieldtextBox;
static IWebElement searchFieldButton;
internal static void TestPreperationForMachTested(IWebDriver webDriver)
{
webDriver.Navigate().GoToUrl("http://www.machtested.com/");
Thread.Sleep(15000);
webDriver.FindElement(By.Id("fclose-button")).Click();//Approve FaceBook Bunner
}
internal static void TestPreperationForAgileQuality(IWebDriver webDriver)
{
webDriver.Navigate().GoToUrl("http://www.agilequalitymadeeasy.com/");
Thread.Sleep(15000);
webDriver.FindElement(By.XPath("/html/body/div[2]/div/div[2]/button[2]")).Click();
}
internal static void LocateSearchFieldInBlog(IWebDriver driver, string elementLocation, string textToAdd)
{
try
{
searchFieldtextBox = driver.FindElement(By.Name(elementLocation));
searchFieldtextBox.SendKeys(textToAdd); //Add Text
searchFieldtextBox.Clear();//Clear Text
searchFieldtextBox.SendKeys(textToAdd); //Re-add test
Console.WriteLine(searchFieldtextBox.GetAttribute("value"));//Get Text in the field
}
catch (NoSuchElementException)
{
Console.WriteLine("Element is not found");
}
}
internal static void LocateButton(IWebDriver driver, string elementLocation)
{
try
{
searchFieldButton = driver.FindElement(By.ClassName(elementLocation));
searchFieldButton.Click();
}
catch (NoSuchElementException)
{
Console.WriteLine("Element is not found");
}
}
}