- Oct 24, 2018
- admin
- 0
Input Box
Input boxes refer to either of these two types:
- Text Fields– text boxes that accept typed values and show them as they are.
- Password Fields– text boxes that accept typed values but mask them as a series of special characters to avoid sensitive values to be displayed.
Entering Values in Input Boxes
To enter text into the Text Fields and Password Fields, you can use sendKeys() method on the WebElement.
using
System;
using
Microsoft.VisualStudio.TestTools.UnitTesting;
using
OpenQA.Selenium;
using
OpenQA.Selenium.Firefox;
namespace
UnitTestProject1
{
[TestClass]
public
class
UnitTest1
{
[TestMethod]
public
void
TestMethod1()
{
IWebDriver driver =
new
FirefoxDriver();
driver.Navigate().GoToUrl(
"https://www.google.com/"
);
IWebElement textBox = driver.FindElement(By.Id(
"lst-ib"
));
textBox.SendKeys("Selenium");
}
}
}
Getting the value from Text Box.
Use GetAttribute(“value”) method to retrieve the value from a text box.
using
System;
using
Microsoft.VisualStudio.TestTools.UnitTesting;
using
OpenQA.Selenium;
using
OpenQA.Selenium.Firefox;
namespace
UnitTestProject1
{
[TestClass]
public
class
UnitTest1
{
[TestMethod]
public
void
TestMethod1()
{
IWebDriver driver =
new
FirefoxDriver();
driver.Navigate().GoToUrl(
"https://www.google.com/"
);
IWebElement textBox = driver.FindElement(By.Id(
"lst-ib"
));
// Doesn't work
string
value = textBox.Text;
// Works
value = textBox.GetAttribute(
"value"
);
}
}
}