×

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 the web element because it is unique.

The Syntax of the Id attribute is as below:

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

Let us see one sample test case in which we will try to identify the web element with the help of ID locators:

Steps Actions Input Expected Result
1. Open the Google Chrome browser.   The Google Chrome browser should be opened.
2. Navigate to the Facebook login page. www.facebook.com The Facebook Login page must be displayed.
3. Identify the username text box and pass the value. Username=admin   The value should be entered in the username text box.
4. Identify the password text box and pass the value. Password=admin The value should be entered in the password text box.
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.
create a new Class File
  • And give your Class name as id_locator and click on the Finish button.
click on the Finish button

 We are creating our test script step by step to give you a complete understanding of how to use ID 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 that how we download it and set System property for the 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

The sample code for the navigation of the desired URL:

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

Step3:

Now we are trying to locate the desired web element by using the value of its id attribute.

  • First, right-click on the username text box and select the Inspect Element field.
we are trying to locate the desired web element
  • The developer tool window will be launched with all the specific codes used in the development of the username text box button.
username text box button
  • Copy the value of id attribute, i.e., email.

Here the sample code:

// identify the username text box and pass the value.
driver.findElement(By.id("email")).sendKeys("admin");
Thread.sleep(2000);
System.out.println("user name entered successfully"); 

Step4:

  • To inspect our next web element, follow the same process as before.
  • Right-click on the password Text box and Inspect Element tab.
password Text box and Inspect Element tab
  • The developer tool window will be launched with all the specific codes used in the development of the password text box button.
  • Then copy the value of its id attribute, i.e., pass.
copy the value of its id attribute

Here the sample code:

// identify the password text box and pass the value.
driver.findElement(By.id("pass")).sendKeys("admin");
Thread.sleep(2000);
System.out.println("password entered successfully ");  

Step5:

In the last step of our sample test script, we will close the existing browser.

Here the sample code for closing the browser,

//Close the browser
driver.close();

Our final test script will look like this:

packagetestpackage;
 import org.openqa.selenium.By;
 import org.openqa.selenium.WebDriver;
 import org.openqa.selenium.chrome.ChromeDriver;
 public class id_locator {
 public static voidmain(String[] args) throws InterruptedException {
 //set the system property for chrome browser
System.setProperty("webdriver.chrome.driver","C:\Users\JTP\Downloads\chromedriver_win32\chromedriver.exe"); 
//create object for chrome driver
WebDriver driver = new ChromeDriver();
// navigate to the url
driver.get("https://www.facebook.com/");
// identify the username text box and pass the value.
driver.findElement(By.id("email")).sendKeys("admin");
Thread.sleep(2000); 
System.out.println("user name entered successfully");
// identify the password text box and pass the value.
driver.findElement(By.id("pass")).sendKeys("admin");
Thread.sleep(2000);
System.out.println("password entered successfully ");
//close the browser
 driver.close();
}
}
  • To run the test script in Eclipse, right-click on the code and then select Run as ? Java Application.
  • The above test script will launch the Google Chrome browser and automate all the test scenarios. 
test script will launch the Google Chrome browser

 

 


Related Topics

Selenium IDE Commands

Selenium –IDE Commands (SELENESE): Selenese commands are the set of commands used in Selenium IDE. Selenese command has two parameters target and value. Selenese command tests the existence of UI elements based on their HTML tags,...

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

Verification Controls Methods in Selenium WebDriver

Verification Controls Methods  The verification controls methods are used to check whether the web element is displayed, enabled, and selected. Some of the most commonly used verification controls are as follow: Verification controls Description Return...

4 minutes read.

WebDriver-Class Name Locator

WebDriver-Class Name Locator In this tutorial, we will learn about the Class name locator of selenium WebDriver. The Class Name locator is used to identify any element in UI based on the...

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

Selenium WebDriver installation

Installation Steps of Selenium WebDriver In this tutorial, we will see the process of selenium webdriver installation. Selenium WebDriver installation process is done in four Stages: Java Installation (version 8 or higher version) Eclipse...

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

Selenium WebDriver-CSS Selector locator A CSS selector is used to identify the web element based on their “# id” and “. Class”, Html tags, and attributes. A CSS stands for Cascading Style...

1 minute read.

CSS Selector: Class in selenium IDE

CSS Selector: Class Identify a web element with the help of Class as a CSS Selector is like using id attribute. The only difference between Class and id is in their...

2 minutes read.

Selenium WebDriver- Radio button handling

Selenium WebDriver- Radio button handling In this tutorial, we will be learning about handling the radio button in selenium WebDriver. Let us take one example to give you a better understanding of...

3 minutes read.

Characteristics of Selenium IDE

Characteristics of Selenium IDE Selenium IDE is separated into different parts, each having their features and functionalities. The seven various components of Selenium IDE are as follows: MENU BAR: The menu bar is...

3 minutes read.

Selenium WebDriver - Firefox or Gecko (Marionette) browser

Selenium web driver-Firefox or Gecko (Marionette) browser In this tutorial, we are going to learn how to run the Selenium WebDriver test script in the Firefox Browser using the Gecko Driver. Before going...

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.

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.

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 – Window handling

Selenium WebDriver – Window handling In this tutorial, we will understand how we can handle the window, multiple windows, and pop-ups in selenium WebDriver. We have different types of window, and pop-ups...

7 minutes read.

Selenium-IDE Installation in Mozilla Firefox

A beginners guides to Selenium-IDE Installation Since Selenium IDE is designed only for Firefox and Chrome plug-in, we are assuming that you have already installed the Mozilla Firefox browser in your...

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