ruby watir to get html of a page
Asked Answered
E

4

11

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

Earthly answered 15/2, 2012 at 22:51 Comment(11)
I am curious as to why you might need the entire html for the page. I ask because a lot of times this is an indication that someone has an rather round-about idea in their head about how to address some testing challenge, and if we know more about what you are trying to do, we might be able to show you an easier more direct way such as getting a smaller portion of the HTML for the thing you are interested in, or learning to use the developer tools for your favorite browser.Paschasia
I 'd like to use nokogiri to parse the html :)Earthly
ok cool, so lets 'pop the why stack' one more time. Why do you want to parse the html with nokogiri?Paschasia
I believe there are cool paring technologies with Watir. But i know nokogiri better (a little bit ).Earthly
I'll grant that, but it doesn't answer the question, so lets pop the why stack one more time. Why do you want to parse the html?Paschasia
find some items I am interested.Earthly
how will you find them (identify them) and what are you going to do with them once found? (although if the idea was to really identify the 'business value' of what you were doing I'd ask why are you interested in those items ;-) )Paschasia
Well, what exactly are you interested in here?Earthly
trying to figure out what you are trying to do or get out of the html and if there could be a short sweet simple way of doing it directly with watir. e.g. a more direct solution to what you are trying to accomplishPaschasia
like I said, there must be some short sweet way of doing parsing html build in with watir. I have very limited knowledge of watir. So after I got the whole html file, I am a bit more comfortable using nokogiri for parsing html :)Earthly
which is sort of what I was trying to help with, but it doesn't seem you are that interested, and doing it via the comment thread is perhaps not the best way. I'd encourage you to post a question with a sample of the portion of the HTML you are interested in, and what you are trying to parse out of it (or accomplish via that parsing) and I and/or others can guide you towards if that is something easily done in watir, and how to do it.Paschasia
C
27

This should do it:

puts browser.html
Check answered 15/2, 2012 at 23:1 Comment(0)
B
3
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
Burkhart answered 1/3, 2015 at 17:20 Comment(0)
H
1

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]')
Hirz answered 27/3, 2013 at 19:40 Comment(0)
O
1

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.

Owenowena answered 1/3, 2015 at 17:17 Comment(1)
The functions show_active and show_links are undefined methods. NoMethodError: undefined method `show_links' for #<Watir::Browser:0x007fd3ea0ab660>Jaundiced

© 2022 - 2024 — McMap. All rights reserved.