我可以编写watir代码来点击这个按钮元素吗?
必须触发onclick事件。
<button onclick="wizardAction('start', 'Starte Upgrade')">
<table align="center" cellspacing="0">
<tbody>
<tr>
<td><img src="/static/images/stock/gtk-ok.png" align="left" height="16" width="16"></td>
<td nowrap="nowrap">Starte Upgrade</td>
</tr>
</tbody>
</table>
</button>
修改
我试过这种方式:
ie.button(:onclick, /start/).click
但似乎不支持 onclick :
> ruby upgrade-module.rb
c:/ruby/lib/ruby/gems/1.8/gems/watir-1.6.5/lib/watir/locator.rb:143:in `match?':
onclick is an unknown way of finding an <INPUT> element ((?-mix:start)) (Watir:
:Exception::MissingWayOfFindingObjectException)
from c:/ruby/lib/ruby/gems/1.8/gems/watir-1.6.5/lib/watir/locator.rb:126
:in `locate'
from c:/ruby/lib/ruby/gems/1.8/gems/watir-1.6.5/lib/watir/locator.rb:124
:in `each'
from c:/ruby/lib/ruby/gems/1.8/gems/watir-1.6.5/lib/watir/locator.rb:124
:in `locate'
from c:/ruby/lib/ruby/gems/1.8/gems/watir-1.6.5/lib/watir/locator.rb:122
:in `catch'
from c:/ruby/lib/ruby/gems/1.8/gems/watir-1.6.5/lib/watir/locator.rb:122
:in `locate'
from c:/ruby/lib/ruby/gems/1.8/gems/watir-1.6.5/lib/watir/locator.rb:119
:in `each'
from c:/ruby/lib/ruby/gems/1.8/gems/watir-1.6.5/lib/watir/locator.rb:119
:in `locate'
from c:/ruby/lib/ruby/gems/1.8/gems/watir-1.6.5/lib/watir/container.rb:8
04:in `locate_input_element'
from c:/ruby/lib/ruby/gems/1.8/gems/watir-1.6.5/lib/watir/input_elements
.rb:5:in `locate'
from c:/ruby/lib/ruby/gems/1.8/gems/watir-1.6.5/lib/watir/element.rb:53:
in `assert_exists'
from c:/ruby/lib/ruby/gems/1.8/gems/watir-1.6.5/lib/watir/element.rb:288
:in `enabled?'
from c:/ruby/lib/ruby/gems/1.8/gems/watir-1.6.5/lib/watir/element.rb:60:
in `assert_enabled'
from c:/ruby/lib/ruby/gems/1.8/gems/watir-1.6.5/lib/watir/element.rb:233
:in `click!'
from c:/ruby/lib/ruby/gems/1.8/gems/watir-1.6.5/lib/watir/element.rb:219
:in `click'
from upgrade-module.rb:19
答案 0 :(得分:6)
其中一个可以工作(未经测试):
browser.image(:src => /gtk-ok/).click
browser.cell(:text => "Starte Upgrade").click
browser.button(:index => 1).click # assumes that it is the first button on the page
browser.button(:xpath => "//button[@onclick='wizardAction(\'start\', \'Starte Upgrade\')']").click
更多信息:
答案 1 :(得分:1)
我遇到过这样的问题。如果不支持onclick属性,则可以执行以下操作。
b.
buttons.
to_a.
select{|but| but.attribute_value(:onclick).match(/start/) }.
first.
click
这只是我从内存中输入的内容,因此可能需要进行一些调整。但是它允许你匹配Watir不一定支持的东西。
我认为还有一些方法可以使用Watir进行XPath查询,你可能也想查看一下