Locators in Selenium Webdriver with Example - How to Locate Element

 What are Locators in selenium - How to locate elements on a webpage?

Selenium Locators 
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. Locator also helps us to identify the objects.

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 selenium webdriver
Locators in Selenium 

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(By.id("login-signin"));//id locator

className Locator 
In Selenium webdriver className locator allow to locate web element 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 to use, the next is worth seeing if the desired element has a name attribute.

Syntax-Basic syntax of Name Locator
WebElement register= driver.findElement(By.name("register"));//Name Locator

TagName Locator 
In Selenium tagName locators are used to locate the element by tagname. tagName is a 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 used to identify the hyperlink on webpage.

Syntax- Basic syntax of link text locator
driver.findElement(By.linkText("Sign Up")).click(); //linkText locator

PartialLinkText Locator 
Partial link text in selenium webdriver is used to locate the hyperlinks using 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 most commonly used locator that is used to navigate through HTML of webpage. xpath is also known as the XML path in selenium web driver.
xPath in selenium
Types of xPath
  • Absolute xPath
  • Relative xPath
Handling complex and dynamic element using xPath locators in selenium
  • Basic xpath
Xpath in selenium Webdriver

  • Contains()
  • OR & AND
  • Starts-with()
  • Text()
axes Method
  • Following
  • Ancestor:
  • Child:
  • Preceding:
  • Following-sibling:
  • parent :
  • Self:
  • Descendant:
CSS Locator 
Use of locators in Selenium
what are the different types of locators in selenium? which one is preferable to use?

Hope !!! Above Tutorial "Locator in Selenium webdriver with example helpful for you ...

QA acharya

Tags : Locators in selenium webdriver ,Use of locators in Selenium ,types of locators in selenium ,xpath locators in selenium , xpath syntax in selenium.how to write xpath in selenium , locators in selenium interview questions

Locators in selenium with example


Post a Comment