Hey @Dushyant, Locating elements in WebDriver is done by using the method “findElement(By.locator())“. You can use following Locators to identify and locate the web elements from a web page in Selenium Webdriver:
-
ID Locator: ID’s are unique for each element so it is common way to locate elements using ID Locator. Also ID locators are the fastest and safest locators out of all locators. For eg. findElement(By.id("IdName"))
-
Name Locator: Name locators are also used to identify the elements on our webpage. Locating elements using Name is same as locating elements using ID locator. These are not unique on a page, so test may fail, if another element with the same Name locator is present on the web page. For eg. findElement(By.name("Name"))
-
Class Name Locator: Class Name locator gives the element which matches the values specified in the attribute name “class”. For eg. findElement(By.className("Element Class"))
-
Tag Name Locator: Tag Name locator is used to find the elements matching the specified Tag Name. It is very helpful when we want to extract the content within a Tag. For eg. findElement(By.tagName("HTML Tag Name"))
-
Link Text Locator: If there are multiple elements with the same link text then the first one will be selected. This Link Text locator works only on links (hyperlinks) so it is called as Link Text locator. For eg. findElement(By.linkText("LinkText"))
-
Partial Link Text: In some situations, we may need to find links by a portion of the text in a Link Text element. it contains. In such situations, we use Partial Link Text to locate elements. For eg. findElement(By.partialLinkText("partialLinkText"))
-
CSS Selector Locator: CSS selectors makes the execution of script faster compared to XPath locator. This locator is always the best way to locate elements on the page. Following are the some of the mainly used formats of CSS Selectors. For eg. findElement(By.cssSelector(tag#id)), findElement(By.cssSelector(tag.class))
-
XPath Locator: XPath is designed to allow the navigation of XML documents, with the purpose of selecting individual elements, attributes, or some other part of an XML document for specific processing. XPath produces reliable locators but in performance wise it is slower compared to CSS Selector. For eg. findElement(By.xpath("XPath"))