ruby watir 获取页面的 html

发布于 2025-01-06 00:06:19 字数 435 浏览 5 评论 0原文

我已经浏览了这些页面上的示例

http://watir.com/examples/ http://wiki.openqa.org/display/WTR/Examples

我还是不知道没有看到获取页面 html 的简单示例。

browser = Watir::Browser.new
browser.goto 'mysite.com'

我已经尝试过

puts browser.text

似乎不起作用。

谢谢

I have looked through the examples on these pages

http://watir.com/examples/
http://wiki.openqa.org/display/WTR/Examples

I still don't see a simple example of getting html of a page.

browser = Watir::Browser.new
browser.goto 'mysite.com'

I have tried

puts browser.text

It seems not working.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

飞烟轻若梦 2025-01-13 00:06:20

这应该可以做到:

puts browser.html

This should do it:

puts browser.html
走野 2025-01-13 00:06:20
puts browser.html

将返回所有 html,如果您只想打印活动对象,您可以使用:

puts browser.show_active

同样,如果您只想打印链接,您可以使用:

puts browser.show_links
puts browser.html

Will return all of the html, in case you only want to print the active objects, you can use:

puts browser.show_active

Similarly if you only want the links to be printed, you can use:

puts browser.show_links
念三年u 2025-01-13 00:06:20

IE8、Ruby 1.9.3、Watir 3.0、WindowsXP

我需要抓取 id="numberCovered" 的单元格中的文本。

<table cellpadding="0" cellspacing="0"  class="thisThemeBodyColor"><tr style="height:22px;"><td id="numberCoveredlabel" style="cursor:default;" class="smallHeadingBlack" width="200">Number of individuals to be covered</td><td id="numberCovered" class="smallHeadingBlack" style="font-weight:bold;">1</td><input type="hidden" name="numberCovered" tooltip="" value="1" onpropertychange="variableAsTextChanged(this);"/></tr><tr><td id="numberSpouseslabel" style="cursor:default;" class="smallHeadingBlack" width="200">Number of spouses to be covered</td><td id="numberSpouses" class="smallHeadingBlack" style="font-weight:bold;">0</td><input type="hidden" name="numberSpouses" tooltip="" value="0" onpropertychange="variableAsTextChanged(this);"/></tr></table>

正如 @icn 提到的,当您找不到合适的 Watir 内置方法时,有时可以使用原始页面源转储作为后备。

- 更新 -
上面提到的 $browser.html 正在喷出空行,但这似乎有效:

require 'nokogiri'
page_html = Nokogiri::HTML.parse($browser.html)
entry = page_html.css('td[id=numberCovered]')

IE8, Ruby 1.9.3, Watir 3.0, WindowsXP

I need to grab the text in a cell with id="numberCovered".

<table cellpadding="0" cellspacing="0"  class="thisThemeBodyColor"><tr style="height:22px;"><td id="numberCoveredlabel" style="cursor:default;" class="smallHeadingBlack" width="200">Number of individuals to be covered</td><td id="numberCovered" class="smallHeadingBlack" style="font-weight:bold;">1</td><input type="hidden" name="numberCovered" tooltip="" value="1" onpropertychange="variableAsTextChanged(this);"/></tr><tr><td id="numberSpouseslabel" style="cursor:default;" class="smallHeadingBlack" width="200">Number of spouses to be covered</td><td id="numberSpouses" class="smallHeadingBlack" style="font-weight:bold;">0</td><input type="hidden" name="numberSpouses" tooltip="" value="0" onpropertychange="variableAsTextChanged(this);"/></tr></table>

As @icn mentioned, a raw page source dump is sometimes nice to have as a fallback when you can't find an appropriate Watir builtin method.

--Update--
The above mentioned $browser.html was spewing empty lines, but this seeems to be working:

require 'nokogiri'
page_html = Nokogiri::HTML.parse($browser.html)
entry = page_html.css('td[id=numberCovered]')
|煩躁 2025-01-13 00:06:20

puts browser.html 将返回页面上的所有对象。如果您只想要活动对象,那么您可以使用puts browser.show_active,类似地,如果您只想显示链接,您可以使用puts browser.show_links,它将显示所有页面上的链接。

puts browser.html will return all the objects on the page. If you want only the active objects then you can use puts browser.show_active similarly if you want only the links to be displayed you can use puts browser.show_links which will show all the links on the page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文