ruby watir获取页面的HTML

时间:2022-02-12 19:13:52

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.

我仍然没有看到获取页面html的简单示例。

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

I have tried

我努力了

puts browser.text

It seems not working.

它似乎不起作用。

Thanks

4 个解决方案

#1


21  

This should do it:

这应该这样做:

puts browser.html

#2


1  

IE8, Ruby 1.9.3, Watir 3.0, WindowsXP

IE8,Ruby 1.9.3,Watir 3.0,WindowsXP

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

我需要在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.

正如@icn所提到的,当你找不到合适的Watir内置方法时,原始页面源转储有时很好地作为后备。

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

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

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

#3


1  

puts browser.html

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

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

puts browser.show_active

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

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

puts browser.show_links

#4


0  

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.

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

#1


21  

This should do it:

这应该这样做:

puts browser.html

#2


1  

IE8, Ruby 1.9.3, Watir 3.0, WindowsXP

IE8,Ruby 1.9.3,Watir 3.0,WindowsXP

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

我需要在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.

正如@icn所提到的,当你找不到合适的Watir内置方法时,原始页面源转储有时很好地作为后备。

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

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

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

#3


1  

puts browser.html

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

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

puts browser.show_active

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

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

puts browser.show_links

#4


0  

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.

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