如何获取水豚中的隐藏元素值? [英] How to get the hidden element value in capybara?

查看:80
本文介绍了如何获取水豚中的隐藏元素值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取一个隐藏元素的值。

I need to get the value of a hidden element.

我尝试了以下代码:

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

返回nil 。

推荐答案

在Capybara 2.1中,您可以将:all 传递给 text 并使用 find('#sample')。text(:all)而不考虑驱动程序。

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

也可以使用匹配器的:text 选项(它们将在内部传递:all 如果:visible false ),则为文本):

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')






在较旧的水豚2.0.x 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
    

    这篇关于如何获取水豚中的隐藏元素值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆