如何使用 Watir (Ruby) 从不可见元素中读取文本? [英] How do I read text from non visible elements with Watir (Ruby)?

查看:28
本文介绍了如何使用 Watir (Ruby) 从不可见元素中读取文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

页面上有一个 div 不可见,但有一些我想捕获的值.在它上面调用 text 会返回一个空字符串.

如何在不处理原始 html 的情况下显示值?无论文本在浏览器中的可见性如何,我都可以强制 .text 返回实际值吗?

irb(main):1341:0>d.first.visible?=>错误的irb(main):1344:0>d.first.html=>"<div class=\"day\">7</div>"irb(main):1345:0>d.first.text=>"

PS:有很多很多div(页面缓存响应并相应地显示它们).我考虑过更改页面中的所有显示:无或单击以使其可见,但如果可能的话,我更愿意避免这种情况.如果不可能,更改所有显示无的解决方案将是首选的解决方法.

PPS:该死,我试图超载可见?Watir::Element 类中的方法总是返回 true,但这并没有奏效.

irb(main):1502:0>d.first.visible?=>真的irb(main):1504:0>d.first.text=>"

解决方案

对于较新版本的 Watir,现在有一个 Element#text_content 方法可以为您执行以下 JavaScript 工作.

>

e = d.firste.text_content#=>7"

对于旧版本的 Watir(原始答案):

您可以使用 JavaScript 来获取此内容.

e = d.firstbrowser.execute_script('返回参数[0].textContent', e)#=>7"

请注意,这仅适用于类似 Mozilla 的浏览器.对于类似 IE 的浏览器,您需要使用 innerText.虽然如果你使用 watir-classic 它只是 d.first.innerText(即不需要 execute_script).

使用 attribute_value:

事实证明,您可以使用 attribute_value 方法使其更简单.好像可以得到和javascript一样的属性值.

d.first.attribute_value('textContent')#=>7"

使用inner_html

如果元素只包含文本节点(即没有元素),也可以使用inner_html:

d.first.inner_html#=>7"

There is a div on a page that is not visible but has some value I want to capture. Calling text on it returns me an empty string.

How do I get the value displayed without having to deal with the raw html? Can I force .text to return me the actual value regardless of the visiblity of the text in the browser?

irb(main):1341:0> d.first.visible?
=> false

irb(main):1344:0> d.first.html
=> "<div class=\"day\">7</div>"

irb(main):1345:0> d.first.text
=> ""

PS: There are many many divs (the page is caching response and display them accordingly). I considered changing all the display:none in the page or clicking to make them visible but I'd prefer to avoid this if possible. If not possible a solution with changing all the display none would be the preferred work around.

PPS: Damned, I tried to overload the visible? method in the Watir::Element class to always return true, but that didn't do the trick.

irb(main):1502:0> d.first.visible?
=> true

irb(main):1504:0> d.first.text
=> ""

解决方案

For the newer versions of Watir, there is now an Element#text_content method that does the below JavaScript work for you.

e = d.first
e.text_content
#=> "7"

For Old Versions of Watir (Original Answer):

You can use JavaScript to get this.

e = d.first
browser.execute_script('return arguments[0].textContent', e)
#=> "7"

Note that this would only work for Mozilla-like browsers. For IE-like browsers, you would need to use innerText. Though if you are using watir-classic it would simply be d.first.innerText (ie no execute_script required).

Using attribute_value:

Turns out you can make it simpler by using the attribute_value method. Seems it can get the same attribute values as javascript.

d.first.attribute_value('textContent')
#=> "7"

Using inner_html

If the element only includes text nodes (ie no elements), you can also use inner_html:

d.first.inner_html
#=> "7"

这篇关于如何使用 Watir (Ruby) 从不可见元素中读取文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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