How to handle text box in Selenium Webdriver

asked 3 months ago

There are different operations we can perform in the text box using the Selenium web driver.

The operations are:

   > Type in the text box: To type, the sendKeys() method is used to pass the Keyboard keys or text into editable elements (text bar, text area) without replacing the previously available content.
   > Editable or not:  isEnabled()  method used to verify if the web element is enabled or disabled within the webpage.
   > Get the text: The getAttribute function is used to get the value
   > Clear: We can clear any editable field using a clear() method present in selenium, most of the time clear() will be used when the text is typed.

Navigate to https://www.qafeast.com/demo, and click the Text box tab.

Press F12 on the keyboard and inspect the Text box.

 

inspect_textbox

 

To type in the text box

To type the text in the text box, the sendKeys() method is used.

Syntax:

driver.findElement(By.locator(" ")).sendKeys("values");

Example:

driver.findElement(By.xpath("//div[@class='form-group'][1]/input[@id='editabletext']")).sendKeys("Admin"); 

 
To check Editable or not:

To check whether the Text box is editable or not, the method .isEnabled() is provided.

Syntax:

driver.findElement(By.locator("")).isEnabled()

Example:

WebElement ele = driver.findElement(By.xpath("//div[@class='form-group'][1]/input[@id='editabletext']"));
if (ele.isEnabled()) {
   System.out.println("Text box is enabled");
} else{
   System.out.println("Text box is not enabled");
}

 

Get the text: To get the text from the text box

To get the typed text in the Text box, getAttribute function is used 

Syntax:

driver.findElement(By.locator("")).getAttribute("value");

Example:

WebElement typedText = driver.findElement(By.xpath("//div[@class='form-group'][1]/input[@id='editabletext']"));
typedText.getAttribute("value");
System.out.println(typedText);

 

To clear the text in the text box:

To clear the Text in the text box clear() method is used.

Syntax:

driver.findElement(By.locator("")).clear();

Example:

driver.findElement(By.xpath("//div[@class='form-group'][3]/input[@id='editabletext']")).clear();

 

Using Selenium Csharp

 

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace ConsoleApp1
{
    internal class SeleniumTest
    {
        static IWebDriver driver;
        static void Main(string[] args)
        {
           
            driver = new ChromeDriver(@"\\Driver\\chromedriver.exe");
            driver.Navigate().GoToUrl("https://qafeast.com/demo");
            //To type in the text box
            driver.FindElement(By.XPath("//div[@class='form-group'][1]/input[@id='editabletext']")).SendKeys("Admin");
            //To check Editable or not:
            IWebElement ele = driver.FindElement(By.XPath("//div[@class='form-group'][1]/input[@id='editabletext']"));
            if (ele.Enabled)
            {
                Console.WriteLine("Text box is enabled");
            }
            else
            {
               Console.Write("Text box is not enabled");
            }
            //Get the text: To get the text from the text box
            IWebElement typedText = driver.FindElement(By.XPath("//div[@class='form-group'][1]/input[@id='editabletext']"));
            Console.WriteLine(typedText.GetAttribute("value"));
            driver.Quit();  
        }
    }
}