Italian Trulli

Locators in Selenium Webdriver with Example - How to Locate Element

Test Tribe

What are Locators in Selenium - How Do You Locate Elements on a Webpage?

Selenium Locators 
A locator in selenium is used to identify the web element on a webpage. To perform an action on the text box, dropdown, alerts, radio button, etc. we can use locators. The locator also helps us to identify objects.

Locator Will Tell you where the HTML component is located on the webpage.

Types of Locators in Selenium

Following is the list of locators supported by selenium 
  • Id
  • ClassName
  • name
  • tagName
  • linkText
  • partialLinkText
  • xpath
  • cssSelector
There are 8 types of locators in the Selenium web driver

Locators in Selenium 

<input type="numberinputmode="numeric" placeholder="Phone" >

input: Tag Name 
Type,inputmode, placeholder: Attribute
"number", "numeric", "Phone": Value of attribute

Id Locator
In selenium, the web driver Id locator is the most widely used locator to locate elements. Id locator is one of the most unique ways of locating web elements.

Syntax- Basic syntax for Id locator 

WebElement searchIcon = driver.findElement("login-signin"));//id locator

className Locator 
In Selenium, the web driver className locator allows locating web elements based on the class value of DOM.

Syntax - Basic syntax of className Locator

WebElement classtest =driver.findElement(By.className(“sample”)); //ClassName locator

Name Locator
In Selenium web driver name locators are used to locate the element using the name attribute. while automating when there is no Id, the next is worth seeing if the desired element has a name attribute.

Syntax-Basic syntax of Name Locator

WebElement register= driver.findElement("register"));//Name Locator

TagName Locator 
In Selenium tagName locators are used to locate the element by tagname. tagName is an HTML tag Eg. input, div, a, etc.

Syntax- Basic syntax of tagName locator

Select select = new Select(driver.findElement(By.tagName("select")));//tagName Locator

LinkText Locator
LinkTextLocator in Selenium is used to identify the hyperlink on the webpage.

Syntax- Basic syntax of link text locator

driver.findElement(By.linkText("Sign Up")).click(); //linkText locator

PartialLinkText Locator 
Partial link text in the Selenium web driver is used to locate the hyperlinks utilizing a portion of their link text.

Syntax- Basic syntax of Partial link text locator

driver.findElement(By.partialLinkText("Sign")).click(); //partial link Text locator

XPath Locators In Selenium

XPath Locator -XML Path
XPath in selenium is the most commonly used locator that is used to navigate through the HTML of the webpage. XPath is also known as the XML path in the Selenium web driver.

Types of XPath

  • Absolute XPath
  • Relative XPath
Handling complex and dynamic elements using XPath locators in selenium
  • Basic XPath
Syntax : //tagname[@attribute='Value']

Xpath in selenium webdriver with Example

  • Contains()
  • OR & AND
  • Starts-with()
  • Text()
axes Method
  • Following
  • Ancestor:
  • Child:
  • Preceding:
  • Following-sibling:
  • parent :
  • Self:
  • Descendant:
CSS Locator 

Types of CSS Locator 

  • Tagname.classname
  • Tagname#id
  • Tagname[attribute'Value']
  • Tag, class, and attribute 
  • Inner text

Use of locators in Selenium
What are the different types of locators in selenium? which one is preferable to use?

Hope !!! The above Tutorial "Locator in Selenium web driver with examples helpful for you ...

QA acharya

Locators in selenium web driver with example

Post a Comment