Wednesday, 1 February 2012

Get value from Textbox in .Net(C#)

Supposed that you have html code something as below:
<input name="outV" onfocus="blur()" type="text" size="23"/>
And you have to get the values from input box mention above. The above input box can be textbox, label or anything.
Then in that case you have to use below piece of code to retrieve the value from Text Box:

//Here I will find the input control using Name, but you can try using Id/css.
IWebElement outPutvalue = WebDriver.FindElement(By.Name("outV"));string currencyValue =outPutvalue.GetAttribute("value") ;
Console.WriteLine("Value from textbox is: " + currencyValue);


Output:

Value from textbox is:  1234



The Whole Piece of code would look like this:

using OpenQA.Selenium;
using OpenQA.Selenium.IE;
//add this name space to access WebDriverWait
using OpenQA.Selenium.Support.UI;
using System.Threading;

public void MyTestInitialize()
 {
  var capabilitiesInternet = new OpenQA.Selenium.Remote.DesiredCapabilities();
  capabilitiesInternet.SetCapability("ignoreProtectedModeSettings", true);
  WebDriver = new InternetExplorerDriver(capabilitiesInternet);
  WebDriver.Navigate().GoToUrl("http://www.x-rates.com/calculator.html");
  IWebElement outPutvalue = WebDriver.FindElement(By.Name("outV"));
  string currencyValue =outPutvalue.GetAttribute("value") ;
   Console.WriteLine("Value from textbox is: " +currencyValue);

}



Thanks,
Md. jawed

3 comments:

  1. string currencyValue = WebDriver.FindElement(By.Name("outV")).GetAttribute("value");

    ReplyDelete
  2. Hi Anony,
    i have done in the same way.
    just to understand in simple and clear i had split it to read the value from textbox..or to make it readable :):)

    ReplyDelete