×

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 - Navigation Controls Commands

Navigation Controls Commands in Selenium WebDriver WebDriver is used to hold the browser history and perform browser navigation operations like, back, forward, and refresh. Whenever a WebDriver launches a browser, all...

5 minutes read.

CSS Selector: Sub String in Selenium IDE

CSS Selector: Sub String CSS Selector Sub-string is used to match a partial string to locate a particular web element. There are three ways to match a substring using a CSS Selector. Match...

2 minutes read.

Selenium WebDriver-CSS Selector: Tag Class and Attribute

Selenium WebDriver-CSS Selector: Tag, Class, and Attribute We will locate the web element with the help of Tag, class, and attribute CSS selector locator in this section of the tutorial. Where Tag,...

5 minutes read.

Mouse and Keyboard Controls in Selenium WebDriver

Mouse and Keyboard Controls in Selenium WebDriver In this tutorial, we will learn about how to handle mouse and keyboard event using action class in selenium WebDriver. Action Class in Selenium WebDriver: Action...

13 minutes read.

Selenium WebDriver Commands

Selenium WebDriver Commands: As we learn previously in the Selenium-IDE tutorial, Selenium commands are the set of commands that are used to run a Selenium test script. But whereas, Selenium WebDriver had...

2 minutes read.

Selenium WebDriver-CSS Selector: Tag and Class

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

4 minutes read.

Selenium Assertion

Selenium Assertion: In this section, we will learn about the Assertion in Selenium WebDriver. An Assertion is a feature available in TestNG, which is used to verify the expected result of...

7 minutes read.

Selenium WebDriver-CSS Selector: Substring Matching

CSS Selector: Substring Matching Selenium WebDriver In this tutorial, we will learn how to identify the web element using the substring method. A Sub-string contains three methods for identifying the web element...

9 minutes read.

Selenium vs QTP

In this section, we are comparing the most frequently used automation tool –Selenium and QTP. Selenium and QTP have relative advantages over each other- FeaturesSeleniumHP QTPLicensingSelenium is an open-source testing tool....

3 minutes read.

Selenium-IDE Installation in Chrome

Selenium-IDE Installation Since Selenium IDE is designed only for Firefox and Chrome plug-in, we are assuming that you have already installed the Google Chrome browser in your system. However, you can...

1 minute 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.

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.

Selenium WebDriver- Microsoft Edge Browser

Selenium WebDriver- Microsoft Edge Browser: In this tutorial, we will learn how to launch the Microsoft Edge browser in the Selenium WebDriver. Before we start automating our test script with Microsoft Edge...

4 minutes read.

WebDriver-Id Locator

WebDriver-Id Locator: The Id Locator is used to identify any web element in UI with the help of the ID attribute. The First preference is always goes to ID locator while inspecting...

4 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.

Selenium Wait

In this tutorial, we will understand how we can avoid synchronization issues while executing the test script in a selenium WebDriver. Before going further in this tutorial, first, we will understand that...

10 minutes read.

Selenium Testing

Selenium is a functional testing tool and compatible with non–functional testing tool. Selenium is free and not required any licensing. There are specified types of testing that can be automated using Selenium:-   Functional Testing: Checking every component...

3 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.

Selenium WebDriver - Browser Controls Commands

Selenium WebDriver - Browser Controls Commands WebDriver includes operations like opening a browser, fetch the title, and closing the browser, etc. Some of the most commonly used Browser controls commands for Selenium WebDriver...

6 minutes read.