×

WebDriver-Name Locator:

WebDriver-Name Locator:

The name locator is used to identify any element in UI with the help of the name backend attribute.

The Syntax of the name attribute is as below:

driver.findElement(By.name(“value of the name attribute”));  

Let us see one sample test case in which we will try to locate the web element using the name locators:

Steps Actions Input Expected Result
1. Open the Google Chrome browser.   The Google Chrome browser should be opened.
2. Navigate to the Google home page. www.google.com The Google home page must be displayed.
3. Identify the search text box and pass the value. Tutorialandexample The value should be entered in the search text box.
4. Click on the Google search button.   The Google search button should be clicked.
5. Close the Browser.   The Browser should be closed.
  • Firstly, we have to launch the Eclipse and open the existing test suite new_test, which we have created in earlier sessions of the WebDriver tutorial.
  • Then right-click on the src folder and create a new Class File from New ? Class.
Then right-click on the src folder
  • And give your Class name as name_locator and click on the Finish button.
give your Class name as name_locator

 We are creating our test case step by step to give you a complete understanding of how to use the name locators to identify a particular web element.

Step1:

  • To access the Google Chrome browser. First, we need to download the Google Chrome driver and set the system property for the Chrome driver.
  • We have already discussed this in previous sessions of the tutorial.
  • And you can also refer given link "Working with Chrome browser” for a better understanding of how we download it and set System property for Chrome driver.
// set the system property for chrome browser
System.setProperty("webdriver.chrome.driver", "C:\Users\JTP\Downloads\chromedriver_win32\chromedriver.exe");
//create the object for chrome driver
WebDriver driver = new ChromeDriver();   

Step2:

After that, we will navigate to the given URL.

Here the sample code,

//navigate to the URL
driver.get("https://www.google.com"); 

Step3:

Now we are trying to locate the Google search text box by using the value of its name attribute.

  • Right-click on the Google search text box and select the Inspect Element tab.
Google search text box
  •  The developer tool window will be launched with all the specific codes used in the development of the Google search text box.
developer tool window will be launched
  • Copy the value of its name attribute, i.e., “q.” 

Here the sample code:

//Identify the search text box and pass the value.
 driver.findElement(By.name("q")).sendKeys("tutorialsandexample");
  Thread.sleep(2000);
 System.out.println("user name entered successfully"); 

Step4:

  • Right-click on the Google search button, and select the Inspect Element tab.
Google search button
  • The developer tool window will be launched with all the specific codes used in the development of the Google search button.
 specific codes used in the development of the Google search button
  • Then, copy the value of its name attribute, i.e., “btnK.”

Here the sample code:

// Click on the Google Search button.
  driver.findElement(By.name("btnK")).click();
 Thread.sleep(2000);
 System.out.println("button is clicked");  

Step5:

In the last step of our sample test case, we are closing the browser.

Here the sample code for closing the browser,

//Close the browser
 driver.close();   

Our final test script will look like this:

package testpackage; 
import java.util.concurrent.TimeUnit; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.chrome.ChromeDriver; 
public class name_locator { 
public static void main(String[] args) throws InterruptedException { 
//set the system property  
System.
setProperty("webdriver.chrome.driver","C:\Users\JTP\Downloads\chromedriver_win32\chromedriver.exe"); //creating the object for chrome driver  WebDriver driver = new ChromeDriver(); //Navigate to google home page.  driver.get("https://www.google.com"); // identify the search text box and pass the value.
driver.findElement(By.
name("q")).sendKeys("tutorialsandexample"); 
Thread.sleep(2000);
System.out.println("value entered successfully ");
// Click on the google search button.
driver.findElement(By.name("btnK")).click();
Thread.sleep(2000);
System.out.println(" button is clicked");  
//Close the browserdriver.close();
}
} 
  • To run the code in Eclipse, we have to right-click on the code and then select Run As ? Java Application.
we have to right-click on the code and then  select Run As
  • The above test script will launch the Google Chrome browser and automate all the test scenarios. 
Google Chrome browser and automate

 

 


Related Topics

Selenium WebDriver Characteristics

Characteristics of Selenium WebDriver Some of the essential characteristics of Selenium WebDriver are as follows: Multiple Operating System Support Selenium Webdriver supports multiple operating systems like: Desktop OS: Window, Linux, and macOS Mobile OS:...

2 minutes read.

Selenium WebDriver – Web Element Controls

Selenium WebDriver – Web Element Controls A web element is an interface that is implemented by a remote web element class. The Web element control can be used to perform the data...

1 minute read.

Selenium WebDriver-CSS Selector: Tag and ID

Selenium WebDriver-CSS Selector: Tag and ID We will locate the web element with the help of tag and id CSS selector in this section of the tutorials Where Tag and id are...

4 minutes read.

XPath Axes Selenium WebDriver

XPath Axes XPath axes are those axes that are used to search for the multiple nodes in the XML document from the current node context. These methods are mainly used when the...

14 minutes read.

Name Locator in Selenium IDE

NAME LOCATOR: It is used to find the name in the web element. If we have multiple web elements of the same name, then the first matched element would be returned. Open the...

3 minutes read.

Xpath locator in Selenium IDE

X PATH LOCATOR: Xpath is used to identify the element using any attribute or visible test. Syntax for X PATH:- //html tag [@attribute=’value’] e.g. - //div [@class=’label’]                    OR //html tag [text () =’Visible Text’] e.g.-//div [text...

2 minutes read.

Selenium IDE Tutorial for Beginners

Selenium IDE Introduction Selenium IDE stands for Integrated Development Environment. Selenium-IDE is released in 2006 and developed by SHINYA [JAPAN] THOUGHTWORK Company. Selenium IDE implemented as a Firefox plug-in, and concedes the record, edit, and debug tests....

2 minutes read.

Id Locator in Selenium IDE

Id Locator Id locator is used to find the web element by the help of id of that element because ID is likely to be unique for each web element. Target Format: Id=id of the...

2 minutes read.

Selenium WebDriver-Alert Popup

Selenium WebDriver-Alert Popup  An alert popup is one of the small message boxes, which used to give some information to the user that displays on the browser. The Alert popup will be...

6 minutes read.

CSS Selector: Inner text in Selenium IDE

CSS Selector: Inner text The inner text is used to identify and create a CSS Selector using a string pattern of the HTML tag which is displayed on the web page. Syntax...

1 minute read.

Selenium-IDE Locators

Selenium-IDE Locators: Before we start learning about Selenium –IDE Locators, firstly we have to learn about UI elements. UI Elements -UI elements are the elements which are displayed on the web application....

1 minute read.

CSS Selector: ID in Selenium IDE

CSS Selector: ID The id attribute is used to identify the web element uniquely in the web pages. Syntax of ID Attribute:  [HTML tag][#][Value of ID attribute] The hash sign (#): it is mandatory while using the...

1 minute read.

Selenium Tutorial

What is Selenium? Selenium is a functional testing tool and compatible with the non-functional testing tools also. Selenium is free and does not require any licensing. Functional tests script executed by...

6 minutes read.

Link Text Locator in Selenium IDE

LINK TEXT Link textlocator is used to identify only the weblink element based on the name of the link. Whenever multiple links are there, it will not work; it will work only...

1 minute read.

WebDriver-Partial Link Text locator

WebDriver-Partial Link Text locator In selenium WebDriver, there is an additional way to find a web link element with the help of partial link text locator. The partial link text is used...

4 minutes read.

WebDriver-Name Locator:

WebDriver-Name Locator: The name locator is used to identify any element in UI with the help of the name backend attribute. The Syntax of the name attribute is as below: driver.findElement(By.name(“value of the...

4 minutes read.

Advantages and Disadvantages of Selenium-IDE

Advantages and Disadvantages of Selenium-IDE Advantages of Selenium IDE: UI based record and playback tool. Easy to understand & developed a test script. Selenium-IDE test script export into web driver code & Selenium–RC It is...

1 minute read.

Selenium WebDriver - IE [Internet Explorer] browser

Selenium WebDriver - IE [Internet Explorer] browser In this segment, we will understand how to run a test script with the help of the IE (Internet Explorer) browser. It is a standalone...

4 minutes read.

Data-Capture Controls Methods in Selenium WebDriver

Data-Capture Controls Methods: The data-capture controls are used to determine the text, size, location, and the CSS value of the web element, etc. Some of the most commonly used data-capture controls are...

7 minutes read.

CSS Selector: ID/Class and Attribute in Selenium IDE

CSS SELECTOR: ID/Class and Attribute The Id, class, and attribute of CSS selector are used to identify the web element to access the web pages.  Syntax for Id/Class and Attribute: css= <./#> {[attribute=Value of...

2 minutes read.