In Selenium WebDriver, everything is related to web elements. All the objects in a web page is called web elements like input box, buttons, etc. To do any action with these web elements, we need to first locate these elements. So to locate these web elements, locators are used. There are 7 types of locators.
Types of locators:-
1) Id: This is the most preferred way to locate the elements on a web page because ID is unique for each web element.
In the above example, this web element(i.e., input box) can be located by its ID i.e., "ID1".
Syntax of using locators: WebElement we= driver.findElement(By.id("ID1"));
2) Name: Elements are also located by their field name.
In the above example(Example 1), web element(input box) can be located by its name i.e., "Demo".
Syntax: WebElement we1=driver.findElement(By.name("Demo"));
3) LinkText: This is used when you know the link within the anchor tag.
For Example3: <a href="www.w3schools.com"> Schools Name </a>
In the above example, web element (i.e., Schools Name link) can be located by its link name "Schools Name"
4) PartialLinkText: This is used when you are locating the element via partial link name.
In the above example(Example3), web element (i.e., Schools Name link) can be located by its partial link name i.e., either by "Schools" or by "Name"
6) className: Locate element by the name of the className.
In the above example(Example1), web element (i.e., input box) can also be located by its class name i.e., "required"
Syntax: WebElement we2=driver.findElement(By.className("required"));
7) Xpath: It is also a type of the locator. Xpath is itself a vast topic. So i'll cover this in my next blog.