Articles → SELENIUM → Locate web elements by name and id in selenium

Locate web elements by name and id in selenium






What does locating a web element mean?





Syntax


// Locating by Id
driver.findElement(By.id("control_id"));
// Locating by name
driver.findElement(By.name("control name"));





Example





Click to Enlarge










Click to Enlarge



package com.FirstSeleniumProject;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class MyClass {

	public static void main(String[] args) {
		WebDriver driver ;
		String service = "IEDriverServer path";
		System.setProperty("webdriver.ie.driver", service);
		driver = new InternetExplorerDriver();
		driver.get("http://gyansangrah.com/ContactUs.aspx");
		// Locating by Id		
    driver.findElement(By.id("ctl00_cphGyanSangrah_txtEmailID")).sendKeys("Test");
		// Locating by name
		driver.findElement(By.name("ctl00$cphGyanSangrah$txtEmailID")).clear();
	}
}



Output





Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Wednesday, March 29, 2017

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250