Selenium – Interacting with Textbox



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");
        }
    }
}

 

Tags: ,
Leave a comment

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

Subscribe now

Receive weekly newsletter with educational materials, new courses, most popular posts, popular books and much more!

https://bridgejunks.com/ https://crownmakesense.com/ https://brithaniabookjudges.com/ https://hughesroyality.com/ https://rhythmholic.com/ https://bandar89.simnasfikpunhas.com/ https://www.100calshop.co.il/products/thailand/ https://myasociados.com/ https://solyser.com/ http://konfidence.cz/ https://muscadinepdx.com/ https://bandar89.parajesandinos.com.ve/ https://goremekoop.com/ https://oncoswisscenter.com/ https://www.turunclifehotel.com/bandar89/ https://www.houseofproducts.biz/ https://taimoormphotography.com/
BIJI18 BIJI18 BIJI18