Locators in Selenium WebDriver :

 

  • Locators provide a way to access the HTML elements from a web page to perform operations on it.
  • W can use locators to perform particular actions on the text boxes, links, check boxes and other web elements to perform operations on it.
  • The locator can be termed as an address that identifies a web element uniquely or may be some times dynamic within the web page. 

 

Locate WebElement from WebPage in WebDriver we locate using below method :

  • Using above mentioned method to locate elements but the locator() part will be replaced with the locator names for e.g: id, name, xPath etc…

 

Methods to Locate WebElement from web page using Different Locators in Selenium WebDriver :

 

By ID: 

  • ID’s are unique for each WebElement so it is common way to locate elements using ID Locator.
  • ID locators are the fastest and safest locators out of all locators.

 

By XPath: 

  • XPath is designed to allow navigation of XML documents, in which the specific elements for specific processing are designed to select individual elements, attributes or some other part of an XML document.
  • XPath produces reliable locator, but this is slow in performance (especially in earlier versions of IE) than the CSS selector.

 

By Name: 

  • These are not unique on one page.
  • If there are many elements with the same name locator then the first element on the page is selected.

 

By ClassName: 

  • The classname locator gives the weblament that matches the values specified in the attribute name “class”.

 

By TagName: 

  • A locator called a tag is used to find elements matching the specified TagName.
  • It is very helpful when we want to extract the content within a tag.

 

By LinkText: 

  • If there are many elements with the same link text then the first will be selected.
  • This LinkText locator works only on links (hyperlinks), so it is called LinkText locator

 

By PartialLinkText: 

  • In some situations, we may have to get a link to a part of the text in the LinkText element, this includes.
  • In such situations, we use partial links to detect elements

 

By cssSelector: 

  • Most automation testers believe that using the CSS selector, the script executes faster than the XPath locator.
  • This locator is always the best way to detect elements on the page.

 

Example of Find Locators in Selenium:

 

Absolute XPath:

  1. Absolute XPath starts with root node of WebPage.
  2. It is also called as complete or full XPath.
  3. Absolute XPath starts from <html> tag.
  4. it starts from single slash(/).

 

Example:

 

Relative Xpath:

  1. A relative xpath is one where the path starts from the node of your choice in WebPage.
  2. it doesn’t need to start from the root node.
  3. It starts with Double forward slash(//).

 

Example:

 

Example of Absolute and Relative Xpath: