Richlewis
Richlewis

Reputation: 15374

if page has selector - Capybara

I'm trying to implement a simple piece of logic that says if element exists on page do something`

The issue I am facing is that if the element doesn't exist then the find method provided returns an exception and will fail my test.

(Capybara::ElementNotFound)

so for example I want to do something like:

if page.find(".element")
  do something
end

If the element doesn't exist then the test should just carry on as normal.

Is there a way to do this?

Upvotes: 1

Views: 2545

Answers (1)

borisano
borisano

Reputation: 1376

Consider using something like this:

if page.has_css?('selector')
  do something
end

This method is described here

Upvotes: 5

Related Questions