Checking the presence of an element in a web page?

Does anyone know how to check the presence of an element in a web page? I need a grroovy example.

FYI exists() doesn’t work because it waits for the configured timeout to end, I need a way to return immediately.

I ended up doing this:

				timeouts(1 * 1000)
				if (exists(byXpath(...)) {
                .
                .
                .
				timeouts(40 * 1000)

Is there any better and more elegant way to do this?

If you need an immediate result, you can do the following:

try {
  $(byXpath('//your-selector'))
} catch(Exception ex) {
}
1 Like

Please also check https://kb.workfusion.com/display/RPA/Simplified+API+Cheat+Sheet#SimplifiedAPICheatSheet-ConditionsandAssertions

It has an example with waiting for an element which can be missing for some time while a page is loading

// set timeout for explicit waits
setFluentWaitTimeout(3 * 1000)
setFluentWaitPollingInterval(200)
 
// check the existence of element - if it might be missing
fluentWait()
  .ignoring(org.openqa.selenium.NoSuchElementException.class)
  .until(ExpectedConditions.presenceOfElementLocated(byCssSelector('#new')))
1 Like