# Locating Elements

## Locating UI Elements <a href="#locating-ui-elements" id="locating-ui-elements"></a>

Automated UI tests (either web or mobile), are mostly focused on performing certain actions on page's or screen's elements. One of the key challenges in writing UI tests is correctly identifying elements on the screen. Accurate element identification is very important to have stable automation tests.

If the elements are incorrectly identified, the test will misbehave and perform actions on wrong elements or just fail, not able to find any. This is called test flakiness. To prevent or minimize flaky tests, you have to make sure the UI elements are uniquely identified.

Selenium and Oxygen use `locators` to identify UI elements. Most of Oxygen commands in `web` and `mob` modules require a locator to perform various actions on the element (such as find an element, assert element's value, etc.). Oxygen provides a wide variety of locator types (strategies) which help to identify elements by different attributes and hierarchical position.

Oxygen allows you to either generate locators automatically or write them manually using various element inspection tools. Here is what you can do:

* **Use Oxygen IDE Recorder** - Oxygen IDE can record Chrome browser based user interactions and generate all possible locators for the selected element. Oxygen IDE will also pre-select the most suitable locator for the specific object. See [Web Recording Guide](https://docs.oxygenhq.org/download-installation-start/start-working-with-oxygen/getting-started-web/recording-web-tests) for more details.
* **Insert Locators Manually** - choose and insert element locators manually in your Oxygen script, using UI element inspection tools like [Chrome DevTools](https://developers.google.com/web/tools/chrome-devtools/), Appium Desktop, etc.

**Note:** In order to record mobile application actions and locators, you will need to use a special version of Appium Desktop that supports generating Oxygen code. See [Mobile Recording Guide](https://docs.oxygenhq.org/download-installation-start/start-working-with-oxygen/getting-started-mobile/recording-mobile-tests) for more details.

### Locator Types <a href="#locator-types" id="locator-types"></a>

Oxygen provides different locator types to help to identify UI elements of web and mobile applications. To indicate which locator type to use, the `locator` parameter shall start with type `prefix`. Below are various locator types and prefixes for web and mobile applications.

#### Web Locator Types <a href="#web-locator-types" id="web-locator-types"></a>

| Locator      | Prefix   | Usage                                                                |
| ------------ | -------- | -------------------------------------------------------------------- |
| ID           | `id=`    | Locates elements by the value of the `id` attribute.                 |
| Name         | `name=`  | Locates elements by the value of the `name` attribute.               |
| Link         | `link=`  | Locates elements by the text in A (anchor) HTML tag.                 |
| CSS Selector | `css=`   | Locates elements via the driver’s underlying W3 CSS Selector engine. |
| XPath        | `xpath=` | Locates elements by XPath.                                           |

See [Web Module (web)](https://docs.oxygenhq.org/download-installation-start/modules/module-web) for more locator types and additional details.

#### Mobile Locator Types <a href="#mobile-locator-types" id="mobile-locator-types"></a>

| Locator | Prefix   | Usage                                                                                                           |
| ------- | -------- | --------------------------------------------------------------------------------------------------------------- |
| ID      | `id=`    | Locates elements by their unique identifier - `resource-id` attribute for android and `name` attribute for iOS. |
| Class   | `class=` | Locates elements by the full name of UI element's class.                                                        |
| Text    | `text=`  | Locates elements by their visible text.                                                                         |
| XPath   | `xpath=` | Locates elements by XPath.                                                                                      |

See [Mobile Module (mob)](https://docs.oxygenhq.org/download-installation-start/modules/module-mob) for more locator types and additional details.

{% hint style="info" %}
Here are some usage examples:
{% endhint %}

```
web.click('id=submit') // ID

web.type('name=firstNameInput', 'first name') // Name

web.click('https://link.com') // Link

web.click('div > button.login') // CSS Selector

web.select('//select[@id="selectGender"]', 'label=female') // XPath
```
