Your ultimate guide

Locators


How to Inspect a web element:
    On every web page, you will find various web elements, and each of these elements is made up of tags, attributes, and values.
    Example: <tag attribute='values'>
  • Right-click any part of the page and Choose Inspect or Shortcut key F12.
  • Select the Element tab (by default it is selected).
  • Note: This inspect tab is called DOM(Document Object Model)
    


What is findElement?
    It is the method to locate elements on the webpage.
What is By?
    By is the mechanism used to locate elements within the document with the help of locator value.
What is a Locator?
    It is an address that identifies web elements within documents with the help of locator value.

Locators: There are eight types of locators.
  1. id
  2. name
  3. XPath
  4. CSS Selector
  5. Class Name
  6. Tag Name
  7. Link Text
  8. Partial Link Text
1. id Locator:
    If an element in your web page has an 'id' attribute, it can be identified using the value of that attribute.

    Syntax (Java): driver.findElement(By.id("id_value"))
    Syntax (C#): driver.FindElement(By.Id("id_value"))
    Syntax (Python): driver.find_element(By.ID, "id_value")

2. name Locator:
    If an element in your web page has a 'name' attribute, it can be identified using the value of that attribute.

    Syntax (Java): driver.findElement(By.name("name_value"))
    Syntax (C#): driver.FindElement(By.Name("name_value"))
    Syntax (Python): driver.find_element(By.NAME, "name_value")

3. XPath Locator:
    XPath is an XML path used for navigation through the HTML structure of the page.
    There are two types of XPath
    1. Absolute XPath
    2. Relative XPath
To learn more about XPath Click Here.

    Syntax (Java): driver.findElement(By.xpath("//xpath"))
    Syntax (C#): driver.FindElement(By.XPath("//xpath"))
    Syntax (Python): driver.find_element(By.XPATH, "//xpath")
Note: we can copy XPath directly from the DOM, Right-Click on the particular element field(on - DOM) then Select copy in and click on Copy XPath.

4. CSS Selector Locator:
    CSS is used to create style rules for web pages and can be used to identify any web element.
To learn how to write CSS Selector Click Here.

    Syntax (Java): driver.findElement(By.cssSelector("value"))
    Syntax (C#): driver.FindElement(By.CssSelector("value"))
    Syntax (Python): driver.find_element(By.CSS_SELECTOR, "value")

5. Class Name Locator:
    If an element in your web page has a 'class' attribute, it can be identified using the value of that attribute.

    Syntax (Java): driver.findElement(By.className("class_value"))
    Syntax (C#): driver.FindElement(By.ClassName("class_value"))
    Syntax (Python): driver.find_element(By.CLASS_NAME, "class_value")

6. Tag Name Locator:
    This Locator is mainly used to get the list of web elements with the particular tag name. We will use this locator very rarely, because here we will assign the values based on the tag, not an attribute. 

    Syntax (Java): driver.findElements(By.tagName("a"))
    Syntax (C#): driver.FindElements(By.TagName("a"))
    Syntax (Python): driver.find_elements(By.TAG_NAME, "a")

7. Link Text Locator:
    Text (Visible Text) used in Hyperlinks can also locate elements.

    Syntax (Java): driver.findElement(By.linkText("text"))
    Syntax (C#): driver.FindElement(By.LinkText("text"))
    Syntax (Python): driver.find_element(By.LINK_TEXT, "text")

8. Partial Link Locator:
    A part of the text(Visible Text) in the Hyperlink can also identify an element.

    Syntax (Java): driver.findElement(By.partialLinkText("text"))
    Syntax (C#): driver.FindElement(By.PartialLinkText("text"))
    Syntax (Python): driver.find_element(By.PARTIAL_LINK_TEXT, "text")




No comments:

Post a Comment