# How to Check if an Element Is Present Using Ocaramba’s Extensions

> Learn to check if an element is present using Ocaramba extensions. Safely verify element visibility with IsElementPresent without exceptions in your WebDriver tests.

- Repository: [Accenture/ocaramba](https://github.com/accenture/ocaramba)
- Tags: how-to-guide
- Published: 2026-02-23

---

**Use the `IsElementPresent` extension method on `IWebDriver` from `Ocaramba.Extensions.WebDriverExtensions` to safely verify element visibility without throwing exceptions.**

The **accenture/ocaramba** framework provides robust Selenium wrappers that simplify common automation tasks. When you need to check if an element is present using Ocaramba, the `IsElementPresent` method offers a reliable way to poll the DOM and return a boolean result rather than handling raw Selenium exceptions in your test code.

## Understanding the IsElementPresent Extension Method

The `IsElementPresent` method is defined in [`OcarambaLite/Extensions/WebDriverExtensions.cs`](https://github.com/accenture/ocaramba/blob/main/OcarambaLite/Extensions/WebDriverExtensions.cs) at lines 111-126. It extends `IWebDriver` to provide a fluent API for presence checks.

The method signature follows this pattern:

```csharp
public static bool IsElementPresent(
    this IWebDriver driver, 
    ElementLocator locator, 
    double timeout)

```

**Key characteristics:**
- Returns `true` only if the element is found **and** its `Displayed` property equals `true`
- Returns `false` if the element does not exist or is not visible within the timeout period
- Internally delegates to `GetElement` from `SearchContextExtensions`

## Prerequisites and Configuration

Before implementing presence checks, you need two core components from the Ocaramba framework.

### ElementLocator

The `ElementLocator` class in [`OcarambaLite/Types/ElementLocator.cs`](https://github.com/accenture/ocaramba/blob/main/OcarambaLite/Types/ElementLocator.cs) encapsulates Selenium locator strategies. It pairs a `Locator` enum value (Id, Xpath, CssSelector, etc.) with the search string.

```csharp
using Ocaramba.Types;

// Define a locator for a login button
private readonly ElementLocator loginButtonLocator = 
    new ElementLocator(Locator.Id, "loginBtn");

```

### BaseConfiguration Timeouts

The `BaseConfiguration` class in [`OcarambaLite/BaseConfiguration.cs`](https://github.com/accenture/ocaramba/blob/main/OcarambaLite/BaseConfiguration.cs) provides centralized timeout values. `IsElementPresent` accepts a `double` representing seconds, typically sourced from:

- `BaseConfiguration.ShortTimeout` (for quick checks)
- `BaseConfiguration.MediumTimeout` (standard waits)
- `BaseConfiguration.LongTimeout` (for slow-loading elements)

## How to Check if an Element Is Present in Ocaramba

### Define an Element Locator

First, declare your locators as readonly fields or properties to promote reusability and maintainability.

```csharp
using Ocaramba.Types;

public class LoginPage
{
    private readonly ElementLocator usernameField = 
        new ElementLocator(Locator.Id, "username");
    
    private readonly ElementLocator submitButton = 
        new ElementLocator(Locator.CssSelector, "button[type='submit']");
}

```

### Perform a Simple Presence Check

Use the extension method directly on your `IWebDriver` instance. Import the `Ocaramba.Extensions` namespace to access the method.

```csharp
using Ocaramba;
using Ocaramba.Extensions;
using NUnit.Framework;

[Test]
public void VerifyLoginButtonIsPresent()
{
    // Check presence using short timeout
    bool isPresent = Driver.IsElementPresent(
        loginButtonLocator, 
        BaseConfiguration.ShortTimeout);
    
    Assert.IsTrue(isPresent, "Login button should be visible on the page.");
}

```

### Check Dynamic Elements with Formatted Locators

For elements with dynamic attributes, use the `Format` method on `ElementLocator` to inject runtime values.

```csharp
// Locator with placeholders
private readonly ElementLocator dynamicMenuItem = 
    new ElementLocator(
        Locator.XPath, 
        "//a[@class='menu-item' and text()='{0}']");

[Test]
public void VerifyDynamicMenuItem()
{
    // Format with specific text
    ElementLocator newsLink = dynamicMenuItem.Format("News");
    
    bool isVisible = Driver.IsElementPresent(
        newsLink, 
        BaseConfiguration.MediumTimeout);
    
    Assert.IsTrue(isVisible);
}

```

### Integrate Presence Checks in Page Objects

Encapsulate presence logic within page object methods to keep tests readable and maintainable.

```csharp
public class DashboardPage
{
    private readonly IWebDriver driver;
    private readonly ElementLocator welcomeMessage = 
        new ElementLocator(Locator.ClassName, "welcome-banner");
    private readonly ElementLocator logoutButton = 
        new ElementLocator(Locator.Id, "logout");

    public DashboardPage(IWebDriver driver) => this.driver = driver;

    public bool IsWelcomeMessageDisplayed() => 
        driver.IsElementPresent(
            welcomeMessage, 
            BaseConfiguration.ShortTimeout);

    public bool IsLogoutAvailable() => 
        driver.IsElementPresent(
            logoutButton, 
            BaseConfiguration.MediumTimeout);
}

```

## How IsElementPresent Works Under the Hood

The implementation in [`WebDriverExtensions.cs`](https://github.com/accenture/ocaramba/blob/main/WebDriverExtensions.cs) relies on `SearchContextExtensions.GetElement` to handle the heavy lifting. The method executes the following logic:

1. **Invokes `GetElement`** with the condition `e => e.Displayed` and the supplied timeout
2. **Returns `true`** immediately when `GetElement` successfully locates a visible element
3. **Catches exceptions** — specifically `NoSuchElementException` and `WebDriverTimeoutException` — and returns `false` instead of propagating the error

This design pattern prevents test failures due to timing issues while providing a clean boolean interface for conditional test logic.

## Summary

- **Use `IsElementPresent`** from `Ocaramba.Extensions.WebDriverExtensions` to verify element visibility without exception handling boilerplate.
- **Define locators** using the `ElementLocator` class with strategies from the `Locator` enum (Id, Xpath, CssSelector).
- **Configure timeouts** via `BaseConfiguration` (ShortTimeout, MediumTimeout, LongTimeout) to control wait duration.
- **Handle dynamic values** by formatting `ElementLocator` instances at runtime with the `Format` method.
- **Encapsulate checks** in page object methods to maintain clean, readable test code.

## Frequently Asked Questions

### What exceptions does IsElementPresent handle internally?

The method catches `NoSuchElementException` and `WebDriverTimeoutException` thrown by the underlying `GetElement` call. When either exception occurs, the method returns `false` rather than propagating the error, allowing your test to continue execution and make conditional assertions based on element presence.

### Can I use custom timeouts with IsElementPresent?

Yes. While the method accepts `BaseConfiguration` timeout values (ShortTimeout, MediumTimeout, LongTimeout), you can pass any `double` value representing seconds. For example, `driver.IsElementPresent(locator, 5.0)` waits exactly five seconds for the element to become present and visible.

### How does IsElementPresent differ from standard Selenium FindElement?

Standard `FindElement` throws a `NoSuchElementException` immediately if the element doesn't exist, requiring explicit try-catch blocks or explicit waits. `IsElementPresent` encapsulates the wait logic and exception handling, returning a boolean result and polling the DOM until the element is both found and displayed (or the timeout expires).

### Is IsElementPresent available for all driver types in Ocaramba?

Yes. Because it extends `IWebDriver`, the method works with any Selenium WebDriver implementation supported by Ocaramba, including ChromeDriver, FirefoxDriver, EdgeDriver, and remote WebDriver instances. The extension method is defined in the `Ocaramba.Extensions` namespace and requires the `OcarambaLite` or full `Ocaramba` package.