How to Find Element in selenium web driver to perform automation. In this tutorial, I am going to cover all the different way to find an element to perform automation using selenium web driver.
findElement(By by): WebElement-WebDriver
 To interact with HTML elements, we need to find them So WebDriver interface has methods like findElement()and findElements() to get element and list of elements. findElement method of WebDriver interface and its returns WebElement.
How to Find Element in selenium web driver
There are several ways to get elements or find an element. Let’s check one by one in detail. How to find Element in selenium web driver.
How to find element By ID in selenium web driver
id(String): By
We can find elements by id. Every element has a unique id but in every case, it’s not necessary so we have other options. It takes a string as a parameter and returns an element to the findElement() method. That string value will we the value of the id attribute in the source code as below image.
Example: Let take the id of a user input field.
WebElement firstName = driver.findElement(By.id("first_name"));
Find element By Name in selenium web driver
name(String): By
Find element by name is also an option we have but we careful name should be unique on the page. It takes string value as a parameter and returns an element object to findElement() method. That string value will we the value of the name attribute in the source code as below image.
WebElement firstNameByName = driver.findElement(By.name("first_name"));
Find element By ClassName in selenium web driver
className(String):By
 To find the element on the basis of the value of the class attribute and in most of the case classes are common for multiple options so its not a better option to find an element. This method takes a string as a parameter. And return an element to findElement method.
  WebElement firstNameByClass = driver.findElement(By.className("form-control"));
Find element By TagName in selenium web driver
tagName(String):By
 This method is used to find the element on the basis of the tag name. It takes an input parameter as a string.
Â
WebElement firstNameByTagName = driver.findElement(By.tagName("input"));
Find element By LinkText & PartialLinkText in selenium web driver
linkText(String):By
 This method is used to find elements on the basis of link Text. Be can take this as a primary option to take element if the text is unique on the page. It also takes a string parameter and returns an element to the findElement method.
WebElement firstNameByTextLink = driver.findElement(By.linkText("privacy policy"));
Find element By XPath in selenium web driver
XPath(String): By
 This is a better option to choose any element. A path is generated by the browser for the element or you can make a custom XPath also. Xpath is used mostly as compared to other options. This method also takes string input and returns an element to the findElement() method.
Difference between findElement() by findElements()
findElement(): findElement() will use to find a single element from the page.
findElements(): findElements() will use to find a list of element from the page.