开发者

how do I dump an html page as XML with watir & JavaScript?

开发者 https://www.devze.com 2023-03-22 06:11 出处:网络
I have been trying to create a simple script in FireWatir that will convert the entire current document DOM\'s (including javascript generated code) to XML representation .

I have been trying to create a simple script in FireWatir that will convert the entire current document DOM's (including javascript generated code) to XML representation .

following leads on the web I've came up with this script

require 'rubygems'
require 'firewatir'


browser = Watir::Browser.new
browser.goto('http://www.google.com/')
browser.text_field(:id, 'lst-ib').set('hello')
browser.button(:name, 'btnG').click
puts browser.execute_script("new XMLSerializer().serializeToString(document)")

however, running it in Firefox 3.6 , resulted in this error :

c:/Ruby192/lib/ruby/gems/1.9.1/gems/firewatir-1.9.2/lib/firewatir/jssh_socket.rb :19:in js_eval': XMLSerializer is not defined (JsshSocket::JSReferenceError) from c:/Ruby192/lib/ruby/gems/1.9.1/gems/firewatir-1.9.2/lib/firewatir/firefox.rb:136:inexecute_script' from test.rb:9:in `'

if I 开发者_开发百科enter this line:

javascript:window.open('aout:blank').document.write('<pre>' + unescape((new     XMLSerializer()).serializeToString(document).replace(/</g, '&lt;')) + '</pre>')

into FF location box, I get a page with the desired XML. so XMLSerializer has to be defined somewhere, its just seems out of reach for my JS code.

how can I get this to work?


Not sure what you mean by "location box", but if that is address bar (the one that says http://stackoverflow.com/... at this page), then try this:

browser.goto "javascript:window.open('aout:blank').document.write('<pre>' + unescape((new     XMLSerializer()).serializeToString(document).replace(/</g, '&lt;')) + '</pre>')"


A t the core of it, I suspect this might be an FF thing to do with boundaries of the 'sandbox' that javascript is running in. The browser itself may know about the serializer, but not choose to give javascript any access to it.

However, there may be more than one way to skin the cat. If your second bit of code provides you with a page that is rendered as text in XML syntax, why not do that first, and then just use the resulting page via

puts browser.text
0

精彩评论

暂无评论...
验证码 换一张
取 消