2012-12-04

How to insert data in Selenium WebDriver

In this article , we will see the functions needed for inserting data in a text box.
Usually the input text fields takes data and send request to server(i.e.- Log In page).
For this , we have to declear an element outside of method.
private IWebElement element;
Now inside any method , (for user name input text box )
-To find the element for taking username as input (in here txtUserName).
element = driver.FindElement(By.Id("txtUserName"));
-To clear existing or inputted text
element.Clear();
-To write user name "Shantonu"
element.SendKeys("Shantonu");

In the same way we can insert a password
And then, find the button and then
-To click the button
element.Click();

So, finally the full code will be
element = driver.FindElement(By.Id("txtUserName"));
element.Clear();
element.SendKeys("Shantonu");
element = driver.FindElement(By.Id("txtPassword"));
element.Clear();
element.SendKeys("123456");
element = driver.FindElement(By.Id("btnSignIn"));
element.Click();

Note - I use C# code.

4 comments:

  1. Hi
    I tried with this example and system show me that unable to locate element :{"method":"id","selector":"txtUserName"}.could you help me.I wanted to automate my yahoo login page as a example.And if u kindly add the screen shot it would be perfect for novice user.
    Thanks for your help.

    ReplyDelete
    Replies
    1. in mail.yahoo , the user name text box id = username, and password text box id= passwd. use firebug to find proper id of the element. for detail , mail me... i can show you screen shot on how to get id using firebug.

      Delete
  2. Hi Shantanu
    Thanks for reply.I made a mistake.Now its ok according to your comment.But if i want to send multiple data for test or read the data from CSV file then what should i do?

    ReplyDelete
    Replies
    1. What do you mean by multiple data here?... a text in a text box in single loading of a page or, more than one time loading of that page?
      Usually, we insert data using send key. If you are using CSV data source, you need to parse CSV file to a variable before inserting data and you can directly use that string variable as send key parameter.

      Delete