Locators for selenium | id locator example | facebook login automation example in java-selenium

Sadakar Pochampalli
JasperSoft BI Suite Tutorials - Sadakar Pochampalli )

Hi,

In this post, you will see the demonstration of "id" locator usage. 

"id" is one of the 8 locators supported by selenium, using it one can navigate to target page by performing click action.

For instance, Login to facebook site @ http://www.facebook.com
Let's see how to identify the ids of login elements for facebook web application.

"Email or Phone" text input HTML
<input type="email" class="inputtext login_form_input_box" name="email" id="email" data-testid="royal_email">

"Password" text input HTML
<input type="password" class="inputtext login_form_input_box" name="pass" id="pass" data-testid="royal_pass">

"Log In" button HTML
<input value="Log In" aria-label="Log In" data-testid="royal_login_button" type="submit" id="u_0_b">


The following statements identifies the above 3 elements using id attribute on the page.
 driver.findElement(By.id("email")).sendKeys("test123@gmail.com");
 driver.findElement(By.id("pass")).sendKeys("testPassword");
  driver.findElement(By.id("u_0_b")).click();.
Take a look at the following example or watch this ~2 min no voice video tutorial for end-to-end execution. 
idDemo.java
package selenium.locators.examples;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

//id demo
public class idDemo {

public static void main(String[] args) {

WebDriver driver
;

System
.setProperty("webdriver.chrome.driver", "D:\\006_trainings\\chromedriver.exe");
System
.setProperty("webdriver.chrome.silentOutput", "true");

//chrome driver object
driver
=new ChromeDriver();

driver
.get("https://www.facebook.com");

//maximizing the URL
driver
.manage().window().maximize();

//finding email element by "id" locator and entering email
driver
.findElement(By.id("email")).sendKeys("test123@gmail.com");

//finding pass element by "id" locator and entering password
driver
.findElement(By.id("pass")).sendKeys("testPassword");

//finding "Login" element by "id" locator and performing click action
driver
.findElement(By.id("u_0_b")).click();

//driver.manage().timeouts().implicitlyWait(20000, TimeUnit.SECONDS);

//driver.quit();

}
}

- Sadakar Pochampalli
Feedback