Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the hidden element value in capybara?

Tags:

ruby

capybara

I need to get the value of a hidden element.

I tried the following code:

page.find(:xpath, "//span[@id='sample']").text

it returns nil.

like image 718
NMKP Avatar asked Feb 25 '13 12:02

NMKP


2 Answers

You can simply find the hidden element and get it's value.

find('#sample', visible: false).value

So simple ;)

like image 70
Nesha Zoric Avatar answered Sep 28 '22 07:09

Nesha Zoric


From Capybara 2.1 you can pass :all to text and use find('#sample').text(:all) regardless of driver.

Also you can use :text option of matchers (they will internally pass :all to text if :visible is false):

page.should have_css('#sample', visible: false, text: 'expected text')

In older Capybara 2.0.x text didn't have such option and returned only visible text.

To return both visible and not visible text you can use in:

  • selenium:
page.evaluate_script("document.getElementById('sample').textContent")
  • rack_test:
find('#sample').native.text
like image 25
Andrei Botalov Avatar answered Sep 28 '22 08:09

Andrei Botalov