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



ADVERTISEMENT
ADVERTISEMENT