Home > Automation Testing > Code to locate element by Name using Selenium Webdriver

Code to locate element by Name using Selenium Webdriver

This post was most recently updated on June 14th, 2019

Locator Type: Name 

  • Name attribute is unique throughout html document except checkbox and radio button. 
  • Name attribute is not unique if multiple radio button and checkboxes used with same name. 
  • Name attribute used with iframe which can be used to switch driver from current web page to iframe by using name. 

Java code to find web element using name attribute value. 

WebElement wElement=driver.findElement(By.name("Name_Attribute_Value")); 

  • Go to www.google.com,
  • Inspect search box
  • Find the value of name attribute.
Name Locator

 

 

Java code to find above element by name attribute is: 

WebElement searchInputBox=driver.findElement(By.name("q")); 

Sample selenium code that uses name locator

Selenium Tutorial >>

This Article is TAGGED in , , . BOOKMARK THE permalink.

Avatar photo
Neeraj Vishwakarma
Senior QA Engineer Skills: Automation Testing, Manual Testing, STLC, Java, Selenium Webdriver, Protractor, Database Testing.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">