×

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 WebDriver- Checkbox handling

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

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.

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.

Drag and Drop Event Handling

Drag and Drop Event Handling: In this tutorial, we will learn how to handle the drag and drop event in selenium WebDriver using the action class. Before going further in this tutorial...

6 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 Tutorial

Seleniun WebDriver Introduction Selenium WebDriver is the essential tool of Selenium Tool Suite because it can directly communicate the browser without any server.  Selenium WebDriver is a browser automation framework that accepts...

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

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

WebDriver Interface | Class Diagram

WebDriver interface/class diagram: The interface of WebDriver includes the following stages, which is as follows: Search Context Web driver Remote web driver Driver class Search Context  The first interface of the web driver class is search Context,...

2 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-CSS Selector: Tag and Attribute

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

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

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.

Selenium WebDriver-Locators

Selenium WebDriver-Locators The WebDriver uses the same set of locating strategies like selenium -IDE for specifying the location of the web element. Each locating strategy has its command in Java to locate...

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

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