Verify Element Presence in Selenium WebDriver

January 30, 2026 · 10 min read · Tool Comparison

Blog / Insights /
Verify Element Presence in Selenium WebDriver

Verify Element Presence in Selenium WebDriver

QA Consultant Updated on

Learn with AI

Linkedin

Facebook

X (Twitter)

Mail

Learn with AI

In Selenium, interact with a page component only works if that element exists. That 's why ensure whether an element is present is one of the first thing any test should do. It substantiate that the application has loaded the expected substance and is ready for the next step.

When you verify element present in Selenium, you reduce flaky failures and improve test dependableness. This is especially helpful when testing dynamical apps that load content based on user activity or net timing.

In this article, we ’ ll cover:

  • What is a web element in Selenium?
  • How to verify element front using different techniques
  • Tips for testing slow-loading or hidden elements
  • Examples using Java and Python
  • How Katalon helps simplify element handling

Let ’ s get into it.

What is an Element in Selenium?

In Selenium, an element refers to any part of a webpage that exploiter can interact with. This includes stimulus battleground, submit buttons, checkboxes, links, images, dropdowns, or still entire sections of content. Each of these components is take a web factor.

To interact with a specific ingredient, Selenium WebDriver habituate locators. These locators name the element inside the Document Object Model (DOM), which is the HTML construction of the page. Locators can be based on tag names, IDs, class names, CSS selectors, or XPath expressions.

Importance of Checking Existence of Element in Selenium

Verifying whether an element is present ensures the tryout runs smoothly. This small step prevents interruptions that can occur when the script essay to interact with something that is n't there. It keeps your mechanization stable and predictable.

For example:

  • Optional elements like cookie streamer, promotional popups, or language selectors may not look for every user. Checking if they exist before interacting allows your trial logic to adapt to different scenarios.
  • Modern web applications often bank on dynamic content that charge after user actions or wait. Instead of race to interact, your script waits or branches logically, bet on what appear on the blind.
  • You can besides design conditional test flows with it. For model, if a certain alert message appears, your tryout handles it. If it does n't, the test moves on. This keeps scripts relevant to real-world demeanour and user experiences.

Methods to Check if an Element Exists in Selenium

Below are practical strategy that match real-world scenario. Each can be enfold in utility functions to keep your code clean and consistent.

Using findElements

This method returns an array. If the raiment is empty, the element is not present. It is simple and safe.

Java
const elements = await driver.findElements (By.css ('button.submit '));
if (elements.length & gt; 0) {
console.log ('Element exists ');
} else {
console.log ('Element not present ');

In Java, this approaching works similarly withfindElementsfrom the Selenium API. It returns an empty list when nothing matches.

Try–Catch with findElement

Using findElementcan shed a runtime error if the component is missing. Wrapping it in a try–catch block offers control and clear handling.

Java
try {
& nbsp; await driver.findElement (By.css ('div.notice '));
& nbsp; console.log ('Element found ');
} catch (err) {
& nbsp; if (err.name === 'NoSuchElementError ') {
& nbsp; & nbsp; console.log ('Element missing ');
& nbsp;} else {
& nbsp; & nbsp; throw err;
  }
}

On the official Selenium corroboration,findElementthrow an exception if nothing lucifer. This construction allow your script handle presence checks graciously.

Explicit Waits (WebDriverWait)

This scheme await until a condition is true, such as when an ingredient appears on screen. It is utile for dynamical UIs and high exam reliability.

JavaScript
& gt; const {until} = require ('selenium-webdriver ');
const timeout = 5000; & nbsp;
await driver.wait (until.elementLocated (By.css ('span.title ')), timeout);
console.log ('Element is now in the DOM ');

Explicit waits are well documented as more true than generic pauses. They allow your playscript to await just the right amount of clip before proceeding.

Pro tip: Tools like SUSA can handle this autonomously — upload your app and get results without writing a single test script.

Using isDisplayed ()

After locating an element, you might want to confirm it is visible. TheisDisplayed ()method provides that chit in one stride.

JavaScript
const constituent = await driver.findElement (By.css (' p.notice ')); if (await element.isDisplayed ()) {console.log ('Element is seeable ');}

This double as presence and visibility verification without deep checks.

Custom Utility Functions

Wrapping any of these strategies in a helper function helps your team stay coherent. Use meaningful names and return booleans for limpidity.

JavaScript
Explain

|

Vincent N.
QA Consultant
Vincent Nguyen is a QA consultant with in-depth domain knowledge in QA, package testing, and DevOps. He has 5+ eld of experience in craft content that resonate with techies at all levels. His interests span from writing, technology, to building nerveless clobber.

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free