Locators
In selenium, locators refers to the technique or method used to identify and locate the web elements within a web page's HTML structure. These elements can include buttons, input fields, hyperlinks, checkboxes, div and many more. Locating elements is a critical step in selenium test automation because it allows you to interact with and manipulate these elements during testing.
Selenium provides several methods for locating and interacting with web elements, and they are typically referred to as locators. Commonly used locators are as follows:
1. ID
This locator uses the HTML id attribute. This should be unique within the web page, to identify an element.
WebElement element = driver.findElement(By.id("elementId"));
IWebElement element = driver.FindElement(By.Id("elementId"));
element = driver.find_element(by=By.ID, value="elementId")
2. Name
This locator uses the HTML name attribute. Name may not always unique, hence it should be cross checked before using this locator.
WebElement element = driver.findElement(By.name("elementName"));
IWebElement element = driver.FindElement(By.Name("elementName"));
element = driver.find_element(by=By.NAME, value="elementName")
3. Class Name
This locator uses the HTML class attribute. A class name will be used on multiple elements, hence this will return multiple element most of the time.
WebElement element = driver.findElement(By.className("elementClassName"));
IWebElement element = driver.FindElement(By.ClassName("elementClassName"));
element = driver.find_element(by=By.CLASS_NAME, value="elementClassName")
4. Tag Name
This locator uses the HTML tag name to locate the element. In a HTML page, a tag might have been used for multiple times. Hence this should be used for a specific requirement where a single tag stands out or used for a collection of similar tags.
WebElement element = driver.findElement(By.tagName("span"));
IWebElement element = driver.FindElement(By.TagName("span"));
element = driver.find_element(by=By.TAG_NAME, value="span")
5. Link Text
This locator is used to locate hyperlinks (anchor tags) by their visible text. Here you need to make sure the link texts are unique.
WebElement element = driver.findElement(By.tagName("Click Here"));
IWebElement element = driver.FindElement(By.TagName("Click Here"));
element = driver.find_element(by=By.LINK_TEXT, value="Click Here")
6. Partial Link Text
This locator is similar to Link Text locator, but matches elements with their partial visible text.
WebElement element = driver.findElement(By.partialLinkText("Click H"));
IWebElement element = driver.FindElement(By.PartialLinkText("Click H"));
element = driver.find_element(by=By.PARTIAL_LINK_TEXT, value="Click H")
7. CSS Selector
CSS selectors are used to apply styles to web elements in HTML. This can also use them to locate elements.
WebElement element = driver.findElement(By.cssSelector("input#username"));
IWebElement element = driver.FindElement(By.CssSelector("input#username"));
element = driver.find_element(by=By.CSS_SELECTOR, value="input#username")
8. XPath
XPath is a powerful language for navigating XML documents, including HTML. You can use XPath expressions to locate elements based on their position, attributes, or text content.
WebElement element = driver.findElement(By.xpath("//input[@id='username']"));
IWebElement element = driver.FindElement(By.XPath("//input[@id='username']"));
element = driver.find_element(by=By.XPATH, value="//input[@id='username']")