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
"Password" text input HTML
"Log In" button HTML
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
<inputtype="email"class="inputtext login_form_input_box"name="email"id="email"data-testid="royal_email">
"Password" text input HTML
<inputtype="password"class="inputtext login_form_input_box"name="pass"id="pass"data-testid="royal_pass">
"Log In" button HTML
<inputvalue="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.javapackage selenium.locators.examples;
importjava.util.concurrent.TimeUnit;
importorg.openqa.selenium.By;
importorg.openqa.selenium.WebDriver;
importorg.openqa.selenium.chrome.ChromeDriver;
//id demo
publicclassidDemo{
publicstaticvoidmain(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