如何使用 watir & 将 html 页面转储为 XML JavaScript?

发布于 2024-11-26 12:33:48 字数 1073 浏览 3 评论 0原文

我一直在尝试在 FireWatir 中创建一个简单的脚本,它将整个当前文档 DOM(包括 javascript 生成的代码)转换为 XML 表示形式。

按照网上的线索,我想出了这个脚本

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)")

,但是在 Firefox 3.6 中运行它,导致了这个错误:

c:/Ruby192/lib/ruby/gems/1.9.1/gems/firewatir-1.9.2/lib /firewatir/jssh_socket.rb :19:in js_eval': XMLSerializer 未定义 (JsshSocket::JSReferenceError) 来自 c:/Ruby192/lib/ruby/gems/1.9.1/gems/firewatir-1.9.2/lib/firewatir/firefox.rb:136:inexecute_script' 来自 test.rb:9:in ` '

如果我在 FF 位置框中输入这一行:

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

,我会得到一个包含所需 XML 的页面。所以 XMLSerializer 必须在某个地方定义,它对于我的 JS 代码来说似乎遥不可及。

我怎样才能让它发挥作用?

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:in
execute_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, '<')) + '</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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烏雲後面有陽光 2024-12-03 12:33:48
browser.goto "javascript:window.open('aout:blank').document.write('<pre>' + unescape((new     XMLSerializer()).serializeToString(document).replace(/</g, '<')) + '</pre>')"

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, '<')) + '</pre>')"
夜无邪 2024-12-03 12:33:48

continue

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文