Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby watir to get html of a page

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

like image 285
icn Avatar asked Feb 15 '12 22:02

icn


3 Answers

This should do it:

puts browser.html
like image 73
Željko Filipin Avatar answered Oct 01 '22 13:10

Željko Filipin


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
like image 28
khan Avatar answered Oct 01 '22 14:10

khan


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]')
like image 30
Chad Brewbaker Avatar answered Oct 01 '22 15:10

Chad Brewbaker