# Locators

&#x20;Locators-These are known as identifiers. Selenium WebDriver uses different locators to find the elements on web page.&#x20;

The different types of locator are:

* &#x20;ID&#x20;
* &#x20;Name &#x20;
* Link Text&#x20;
* &#x20;CSS Selector&#x20;
* &#x20;DOM (Document Object Model)&#x20;
* &#x20;XPath

#### &#x20;ID

This is the most common way of locating elements since ID's are supposed to be unique for each element. Target Format: id=id of the element \
Example: Id=email

#### &#x20;Name

Locating elements by name are very similar to locating by ID.But multiple element can have the same name property. When element have the same property it always identify very first element with that property. \
**Target Format:** name=name of the element Example: Name=username\
&#x20;**Locating by Name using Filters** Filters can be used when multiple elements have the same name. Filters are additional attributes used to\
&#x20;**distinguish elements with the same name.**\
&#x20;**Target Format**: name=name\_of\_the\_element filter=value\_of\_filter .\
**Example:** name=tripType value=oneway

&#x20;**Link Text-** This type of locator applies only to hyperlink texts.&#x20;

**Target Format:** link=link\_text \
**Example :** Link=REGISTER

&#x20;**CssSelector- CssSelecto**r is the most common locating strategy of advanced Selenium users because it can access even those elements that have no ID or name.&#x20;

It is used to identify an element based on a combination of HTML tag, id, class, and attributes.&#x20;

**CSS Selectors have many formats like:**

* Tag and ID&#x20;
* Tag and class
* Tag and attribute&#x20;
* Tag, class, and attribute&#x20;
* Inner text

#### &#x20;Locating by CSS Selector - Tag and ID&#x20;

Target Format: css=tag#id tag- the HTML tag #- This should always be present when using a CSS Selector with ID id- the ID of the element Example- css=input#email&#x20;

#### Locating by DOM (Document Object Model)

&#x20;There are four basic ways to locate an element through DOM:

* &#x20;getElementById
* &#x20; getElementsByName&#x20;
* &#x20;dom:name (applies only to elements within a named form)
* &#x20; dom:index

#### &#x20;Locating by DOM – getElementById&#x20;

**Target Format:** document.getElementById("id of the element")\
&#x20;**Example**-document.getElementById("persist\_box")\
&#x20;

#### Locating by DOM - getElementsByName&#x20;

**Target Format:** document.getElementsByName("name")\[index] \
**Example**-document.getElementsByName("servClass")\[0]&#x20;

#### Locating by DOM - dom:name&#x20;

**Target Format:** document.forms\["name of the form"].elements\["name of the element"] **Example-**&#x64;ocument.forms\["home"].elements\["userName"]

#### &#x20;Locating by DOM - dom:index&#x20;

**Target Format**: document.forms\[index of the form].elements\[index of the element]\
**Example**- document.forms\[0].elements\[3] (Phone No Field) \
**Example-**&#x64;ocument.forms\[0].elements\["phone"]

![](https://106491783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTW0eWU2biq082hmKEn%2F-LTksJkhI5sOP2qddetl%2F-LTkvwyNoa3nghbYuc5K%2Fimage.png?alt=media\&token=7a99d360-b982-42fe-92d5-6b46d464f5df)

![](https://106491783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTW0eWU2biq082hmKEn%2F-LTksJkhI5sOP2qddetl%2F-LTkw0EAN4NtbrliZmUE%2Fimage.png?alt=media\&token=98a95244-71b8-4a13-962c-47a74c54ff7c)
