Michael P.
Michael P.

Reputation: 1413

Capybara::ElementNotFound error - Super simple Capybara example is failing and I have no clue as to why

Here's my code. I've been messing around with this for 1.5 hours and have yet to get Capybara to click a link without the ElementNotFound error. Visiting the site works as expected, but clicking links, filling in forms--really, interacting with the DOM in any fashion--fails. Any help would be greatly appreciated.

require 'capybara'
require 'capybara/dsl'

class Prowler
  include Capybara::DSL

  def initialize
    Capybara.run_server = false
        Capybara.default_driver = :selenium
  end

  def visitSite
    session = Capybara::Session.new(:selenium)
    session.visit "https://www.cnn.com"
    click_link 'Entertainment'
  end
end

prowler = Prowler.new
prowler.visitSite

Upvotes: 0

Views: 47

Answers (1)

Thomas Walpole
Thomas Walpole

Reputation: 49850

It's failing because you're not calling click_link on the session where you visited a URL. By including Capybara::DSL you've made all of Capybaras methods available on the object, and when you call click_link you are actually calling Capybara.current_session.click_link. However you've created your own session which isn't Capybara.current_session, so you either need to let Capybara manage the session (which in your use case probably won't work well since it seems like you want a session per class instance)

visit 'www.cnn.com'
click_link 'Entertainment'

or don't include Capybara::DSL and instead manage you're own session per class instance like you are doing and call the methods on the session

session = Capybara::Session.new(:selenium)
session.visit "https://www.cnn.com"
session.click_link 'Entertainment'

see https://github.com/jnicklas/capybara#using-sessions

Upvotes: 1

Related Questions