title: How to parse forms with Mechanize or Nokogiri from a string
body: <p>I need to parse the form to get the value of `IW_SessionID_from the HTML I get back, which I can't get to work.</p>
  
  <pre><code>#!/usr/bin/ruby
  
  require 'pp'
  require 'nokogiri'
  require 'mechanize'
  
  r = '&lt;HTML&gt;&lt;HEAD&gt;&lt;TITLE&gt;&lt;/TITLE&gt;&lt;meta http-equiv=\"cache-control\" content=\"no-cache\"&gt;\r\n&lt;meta http-equiv=\"pragma\" content=\"no-cache\"&gt;\r\n&lt;NOSCRIPT&gt;&lt;HTML&gt;&lt;BODY&gt;Your browser does not seem to support JavaScript. Please make sure it is supported and activated&lt;/BODY&gt;&lt;/HTML&gt;&lt;/NOSCRIPT&gt;\r\n&lt;SCRIPT&gt;\r\nvar ie4 = (document.all)? true:false;\r\nvar ns6 = (document.getElementById)? true &amp;&amp; !ie4:false;\r\nfunction Initialize() {\r\nvar lWidth;\r\nvar lHeight;\r\nif (ns6) {\r\n  lWidth = window.innerWidth - 30;\r\n  lHeight = window.innerHeight - 30;\r\n} else {\r\n   lWidth = document.body.clientWidth;\r\n   lHeight = document.body.clientHeight;\r\n   if (lWidth == 0) { lWidth = undefined;}\r\n   if (lHeight == 0) { lHeight = undefined;}\r\n}\r\ndocument.forms[0].elements[\"IW_width\"].value = lWidth;\r\ndocument.forms[0].elements[\"IW_height\"].value = lHeight;\r\ndocument.forms[0].submit();\r\n}&lt;/SCRIPT&gt;&lt;/HEAD&gt;&lt;BODY onload=\"Initialize()\"&gt;\r\n&lt;form method=post action=\"/bwtem\"&gt;\r\n&lt;input type=hidden name=\"IW_width\"&gt;\r\n&lt;input type=hidden name=\"IW_height\"&gt;\r\n&lt;input type=hidden name=\"IW_SessionID_\" value=\"1wqzj1f0vec57r1apfqg51wzs88c\"&gt;\r\n&lt;input type=hidden name=\"IW_TrackID_\" value=\"0\"&gt;\r\n&lt;/form&gt;&lt;/BODY&gt;&lt;/HTML&gt;'
  
  page = Nokogiri::HTML r
  puts page.css('form[name="IW_SessionID_"]')
  
  a = Mechanize.new
  page2 = Mechanize::Page.new(nil,{'content-type'=&gt;'text/html'},r,nil,a)
  
  pp page2.form_with(:name =&gt; "IW_SessionID_")
  </code></pre>
  
  <p>The script just returns <code>nil</code>.</p>
  
  <p>Can anyone figure out how to get the value of <code>IW_SessionID_</code>?</p>