Locators in Selenium | Explain Different Locators in Selenium?




 In Selenium, locators are used to identify and locate elements on a web page. The elements can be buttons, links, text fields, etc. 


Selenium provides several types of locators to identify elements:


1. ID: This is the most preferred way to locate an element. An ID is a unique identifier for an element on a web page and it is usually assigned by the developer. 

You can locate an element by its ID using the findElement() or findElements() method and pass in the By.id() method.


driver.findElement(By.id("someId"));



2. Name: Similar to the ID, the name attribute is also an identifier assigned by the developer. It is not always unique and can be shared by multiple elements. You can locate an element by its name using the findElement() or findElements() method and pass in the By.name() method.


driver.findElement(By.name("someName"));




3. Class Name: It allows you to locate an element by its class name. Like the name attribute, it is not always unique and can be shared by multiple elements. You can locate an element by its class name using the findElement() or findElements() method and pass in the By.className() method.


driver.findElement(By.className("someClass"));




4. Tag Name: you can locate an element by its tag name. For example, if you want to locate all the anchor tags on a page, you can use the findElements() method and pass in the By.tagName() method.


driver.findElements(By.tagName("a"));




5. Link Text: Allows you to locate a link by its text. You can use the findElement() method and pass in the By.linkText() method.


driver.findElement(By.linkText("Google"));




6. Partial Link Text: Allows you to locate a link by a portion of its text. You can use the findElement() method and pass in the By.partialLinkText() method.


driver.findElement(By.partialLinkText("Goo"));




7. CSS Selector: Allows you to locate an element by its CSS Selector. You can use the `findElement() or findElements() method and pass in the By.cssSelector() method. The CSS Selector is a string that describes the location of an element using the element's class, id, and attributes. 

For example, to locate an element with a specific class, you can use the. notation, like this:


driver.findElement(By.cssSelector(".someClass"));




8. XPath: Allows you to locate an element by its XPath. XPath is a language used to navigate XML documents, including (but not limited to) HTML. It allows you to select elements based on their attributes and position in the document. 

You can use the findElement() or findElements() method and pass in the By.xpath() method.


driver.findElement(By.xpath("//input[@name='someName']");



In summary, Selenium provides several types of locators to identify elements on a web page, such as ID, name, class name, tag name, link text, partial link text, CSS selector, and XPath. Each locator has its own advantages and disadvantages, and the best locator to use depends on the specific situation. Generally, it's recommended to use the ID or name attribute as the first option, and then fallback to other options if necessary.

Post a Comment

Post a Comment (0)

Previous Post Next Post