ghal ghal - 2 months ago 13
Ruby Question

Nokogiri is not loading "browser" from Watir

I have this so far:

require 'watir-webdriver'
require 'date'
require 'nokogiri'

browser = Watir::Browser.start 'https://example/ViewReport.aspx'

browser.link(:text, 'Combined Employee Performance Report').click

today = Date.today
yesterday = today.prev_day.strftime('%m' '%d' '%Y')

t = browser.text_field :id => 'UC255_txtStart'
t.set yesterday

t = browser.text_field :id => 'UC255_txtEnd'
t.set yesterday

btn = browser.button :value, 'Run Report'
btn.exists?
btn.click

page = Nokogiri::HTML.parse('browser')
links = page.css("a")
puts links.length


When I try to parse
browser
, the variable that Watir is using for the site URI, it gives me a blank HTML page.

Answer

Problem

When you do

page = Nokogiri::HTML.parse('browser')

You are asking Nokogiri to parse the string 'browser'.

Solution

What you actually want to parse is the html in the browser.

To get the browser's html, you do:

browser.html

So to parse it, you would do:

page = Nokogiri::HTML.parse(browser.html)