# Find Element(s)

{% hint style="success" %}
The **findElement** method in Selenium WebDriver is used to locate a single WebElement on a web page.
{% endhint %}

## Key things about findElement:

* It is a method that belongs to the WebDriver interface in Selenium.
* It takes a locator strategy such as By.id, By.xpath, By.cssSelector, etc as a parameter to identify the element.
* It returns a WebElement object, which represents a DOM element on the page.
* Only finds the first matching element.
* If no element matches the locator, it throws a NoSuchElementException.
* The related method is findElements (plural) which returns all matching elements.

<figure><img src="https://images.unsplash.com/photo-1561380851-39b27c4f1626?crop=entropy&#x26;cs=srgb&#x26;fm=jpg&#x26;ixid=M3wxOTcwMjR8MHwxfHNlYXJjaHwxfHxmaW5kfGVufDB8fHx8MTY4OTczMzU0OXww&#x26;ixlib=rb-4.0.3&#x26;q=85" alt=""><figcaption></figcaption></figure>

### 🎯 Rules of findElement

* Accepts locator strategies like id, xpath, cssSelector 👍
* Returns first matching WebElement object 🔍
* Throws NoSuchElementException if no match 🚨
* WebElement can call click(), sendKeys() etc. ✅

```java
WebElement searchBar = driver.findElement(By.id("search")); 
searchBar.sendKeys("Selenium");
```

### ⚙️ Relation to OOPs Concepts ⚙️

#### 🦾 Abstraction

* WebDriver defines abstract findElement method
* Concrete classes like ChromeDriver implement it

#### 💡 Polymorphism

* Supports poly morphic behavior based on WebDriver reference but ChromeDriver object

#### 👪 Inheritance

* ChromeDriver, FirefoxDriver inherit findElement from WebDriver

#### 🏭 Class

* WebElement class encapsulates reusable methods like click(), sendKeys()

### 📏 Best Practices

* Use unique locator strategies like ID or xpath 🎯
* Wrap findElement in try/catch block 🚧
* Avoid repeating findElement. Store once and reuse 🔂

So findElement leverages OOPs concepts like abstraction, polymorphism, inheritance to enable element interaction!

## Some example usage in Java:

```java
// Find element by ID
WebElement elem = driver.findElement(By.id("myElement"));

// Find element by XPath 
WebElement elem = driver.findElement(By.xpath("//div[@class='myclass']"));

// Interact with returned WebElement
elem.click(); 
elem.sendKeys("Text");
```

## So in summary:

* findElement allows locating a single element on the page using a locator strategy
* It returns a WebElement object representing that DOM element
* This enables further interactions like clicking, entering text, etc.
* It is one of the most common Selenium commands for web automation testing

## Find Elements

The `findElements()` method in Selenium Java is used to find a list of web elements that match a given locator. The return type of the `findElements()` method is a `List<WebElement>`, which is a collection of web elements.

#### The syntax for the `findElements()` method is as follows:

```
List<WebElement> elements = driver.findElements(By.locator);
```

where `driver` is the WebDriver instance and `locator` is the locator that is used to identify the web elements.

For example, the following code would find all the `input` elements with the `type` attribute of `text` on the current web page:

```
List<WebElement> textInputs = driver.findElements(By.cssSelector("input[type='text']"));
```

The `findElements()` method can be used to find a list of web elements that match a variety of locators, including:

* ID
* Name
* Class name
* XPath
* CSS selector

The `findElements()` method is often used in Selenium automation scripts to find a list of web elements that need to be interacted with. For example, a script might use the `findElements()` method to find all the `button` elements on a web page and then click on each button.

## Here are some of the differences between `findElement()` and `findElements()` methods:

* `findElement()` returns a single web element, while `findElements()` returns a list of web elements.
* `findElement()` throws a `NoSuchElementException` if the element is not found, while `findElements()` returns an empty list if the elements are not found.
* `findElement()` is typically used when you need to interact with a single web element, while `findElements()` is typically used when you need to interact with a list of web elements.

**findElement():**

* Finds a single element on the page. If it finds more than one matching element, it will throw an exception.
* Returns a WebElement object which can be used to interact with the dom element.

**findElements():**

* Finds all matching elements on the page and returns a List of WebElement objects.
* It will return an empty list if no elements are found. It will not throw an exception if more than one match is found.
* You can then iterate over the list of WebElements and interact with each element.

So in short:

**findElement()** - Finds a single element, and throws an exception if more than one is found. Returns a WebElement.

**findElements()** - Finds all matching elements, and returns a List of WebElements. Returns an empty list if no match is found.

Some usage examples:

Find a single element by ID:

```java
WebElement element = driver.findElement(By.id("someId"));
```

Find multiple elements by class name and iterate:

```java
List<WebElement> elements = driver.findElements(By.className("someClass"));
for (WebElement element : elements) {
    element.click();
}
```

Find a single element but use a list in case more than one is found:

```java
List<WebElement> elements = driver.findElements(By.id("someId"));
WebElement element = elements.get(0);
```

This will not throw an exception if more than one match is found, it will simply pick the first element from the list.

If there are no matching elements within the web page, findElements returns an empty list, whereas if there is no element found findelement will return **noSuchElementException.**
