Tuesday 30 September 2014

Selenium C# : Insert text in website textbox with selenium C# webdirver

Click Here to Download InsertTextInTextBox.zip

Note : If any runtime error occurred by running our downloaded project meaning of that your browser version and Selenium API DLL version is not matching. To resolve this issue you need to download latest Selenium C# API DLLs from this link and paste downloaded DLLs into 'Bin' folder of downloaded project. Or you can remove references of existing DLLs references and add new DLLs references. You can learn this thing from this link.


Using selenium C# webdriver API we are able to insert text in website textbox element automatically in web browser. This article is very useful for automate website testing.

To enter text in textbox we need locator string of that particular textbox. To get locator string you need to your FireFox Element Locator Add-on.

Here is example for insert text in textbox automatically:
In this example first we open website 'www.google.com'. After that, we locate search textbox using locator add-on. Now in C# code we create object of FireFoxDriver Class.

We are using 'FindElement' method of 'FireFoxDriver' Class object, in this object we are finding element using 'XPath'. We are using 'OpenQA.Selenium.By.XPath' method. After finding element we are using 'SendKeys' method to insert text in textbox.

In 'SendKeys' method's parameter pass your desire text or string. This method exist in 'OpenQA.Selenium.IWebDriver.ISearchContext.FindElement' class.

Selenium C# Code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Selenium;
using OpenQA;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        OpenQA.Selenium.IWebDriver objFF = new OpenQA.Selenium.Firefox.FirefoxDriver();
        objFF.Navigate();
        objFF.Url = "http://www.google.com";
        objFF.FindElement(OpenQA.Selenium.By.XPath("//input[@id='gbqfq']")).SendKeys("ASP.NET");
                
    }
}

[Image : Open 'www.google.com' and enter text] :
 
Selenium C# : Insert text in website textbox with selenium C# webdirver
(To view original size image , click on image)

Prerequiest to learn this article :

You need to learn these previous articles before learning this article.

Selenium Tool : Configure selenium tool for asp.net C# website or project in Visual Studio.
Selenium Tool : Download and Install WebDriver Element Locator Add-on.

Full Video : 



4 comments:

  1. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.



    selenium training in chennai


    java training in chennai

    ReplyDelete
  2. Nice post,and good information Thanks for sharing further check it once at
    .NET Online Training Hyderabad

    ReplyDelete
  3. Hi, I am getting error while execute Sendkeys in selenium after finding element using XPath. Below are the code statements,
    IWebElement objElement = ((IWebDriver)clsRunConfiguration.objBrowser).FindElement(By.XPath(sDOMXPath));
    objElement.SendKeys(sValueToBeSet); //Error is "Invalid Argument, text must be string\n"
    Need urgent help to resolve this issue.

    Regards,
    Nikunj

    ReplyDelete