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.
You can simply find the hidden element and get it's value.
find('#sample', visible: false).value
So simple ;)
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:
page.evaluate_script("document.getElementById('sample').textContent")
find('#sample').native.text
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With