Selenium Webdriver Element Locator - Locating Element By Name With Example

Selenium WebDriver/Selenium 2 is web and mobile software application regression testing tool and it is using element locators to find out and perform actions on web elements of software application. We have learn Element Locating by ID, Element Locating By Tag Name and Locating element by Class Name with examples in my previous posts. Locating Element By Name is very useful method and many
peoples are using this method in their webdriver software automation test case preparation. Locating Element By Name and Locating Element by ID are nearly same. In Locating Element by ID method, webdriver will look for the specified ID attribute and in Locating Element By Name method, webdriver will look for the specified Name attribute on page of software web application.

Let me show you one example of how to locate element by name on page of software web application and then we will use it in our webdriver test case for software web application.


Look in to above image, web element 'First Name' input box do not have any ID so webdriver can not locate it By ID but that element contains name attribute. So webdriver can locate it By Name attribute. Syntax of locating element in webdriver is as bellow.

driver.findElement(By.name("fname"));

Now let we use it in one practical example as bellow.Copy bellow given @Test method part and replace it with the @Test method part of example given on this page.
(Note : @Test method is marked with pink color in that example).

@Test
 public void test() 
  {
   driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
  //Locating element by Name and type given texts in to input box.
   driver.findElement(By.name("fname")).sendKeys("My First Name");
  }

Above example will type the text in text box using By Name element locating method.

6 comments:

  1. How to email as text in textbox.

    ReplyDelete
    Replies
    1. Write email address at place of text with sendkeys

      Delete
    2. You can use the same send.keys and as per above reference you can use :

      driver.findElement(By.id("email")).sendKeys("neerajsharma.001@gmail.com");

      Delete
  2. How to type email in the textbox using selenium

    ReplyDelete
    Replies
    1. driver.findElement(By.Xpath("//*[@id='Email']")).Senskey("Email Id");

      Delete
  3. Please Tell me how to enter Numeric value in Text Box.

    ReplyDelete