使用 watir 访问 <body> HTML 元素

Access <body> HTML element using watir

你好我想写邮件,邮件的文本区域有以下HTML代码

任何人都可以帮助我如何使用 watir 访问它我已经完成了以下步骤但无法获取它

irb
require 'rubygems'
require 'watir'

@ie.body(:id,':158').set 'text'

我的系统配置是

IE-8
Windows-7

    <div class="Ar Au" id=":88" style="display: block;">
    <iframe tabIndex="1" class="Am Al editable" id=":83" src="html/compose/static_files/blank_quirks.html" frameBorder="0" style="padding-bottom: 0px; background-color: white; padding-left: 0px; padding-right: 0px; height: 245px; overflow: visible; padding-top: 0px;" allowTransparency="allowtransparency" closure_lm_423419="null">
<body class="editable LW-avf" id=":158" role="textbox" contentEditable="true"
    hideFocus="hidefocus" closure_lm_478727="[object Object]" g_editable="true"/>

对于不是输入的可编辑元素,即本例中的 body 元素,没有 set 方法。对于这些类型的元素,您需要使用 send_keys 方法。假设当前版本的 Watir,以下将起作用:

@ie.iframe(:id => ':83').body(:id, ':158').send_keys('text')

由于 iframe 中应该只有一个 body 元素,您还可以这样做:

@ie.iframe(:id => ':83').body.send_keys('text')

但是,鉴于您使用的是 Watir v1.6.7,其中一些方法不同或不存在。例如,没有 iframebody 方法。同样,元素没有 send_keys 方法。相反,您需要将焦点放在 body 元素上,然后 send_keys 到浏览器。

@ie.frame(:id, ':83').element(:id, ':158').focus
@ie.send_keys('text')

请注意,元素需要处于焦点位置,因此执行此操作时不要将焦点从浏览器上移开。