Locators in Selenium Webdriver with Example - How to Locate Element
What are Locators in Selenium - How do 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. Locator also helps us to identify the objects.
Following is the list of locators supported by selenium
- Id
- ClassName
- name
- tagName
- linkText
- partialLinkText
- xpath
- cssSelector
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 locating web element based on the class value of DOM.
WebElement classtest =driver.findElement(By.className(“sample”)); //ClassName locator
Name Locator
In Selenium web driver name locators are used to locating 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.
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 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
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 selenium webdriver is used to locate the hyperlinks using 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
- Basic XPath
Xpath in selenium Webdriver with Example |
- Contains()
- OR & AND
- Starts-with()
- Text()
- Following
- Ancestor:
- Child:
- Preceding:
- Following-sibling:
- parent :
- Self:
- Descendant:
Use of locators in Selenium
what are the different types of locators in selenium? which one is preferable to use?
Team,
QA acharya
Tags: Locators in selenium web driver, 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
Post a Comment
0 Comments