Locators in Selenium Webdriver with Example - How to Locate Element

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

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

Types of Locator in selenium
Following are the list of locators supported by selenium 
  • Id
  • ClassName
  • name
  • tagName
  • linkText
  • partialLinkText
  • xpath
  • cssSelector
There are 8 type of locator in selenium webdriver
Locators in Selenium 

Id Locator
In selenium webdriver Id locator is most widely used locator to locate element. Id locator is one of most unique way of locating web element.

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 webdriver name locator are used to locate the element using name attribute. while automating when there is no Id to use, the next 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 locator are used to located 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