How to click a button element in watir?
Can I write watir code to click this button element?
The onclick event has to be triggered.
<button onclick="wizardAction('start', 'Starte Upgrade')">
<table align="center" cellspacing="0">
<tbody>
<tr>
<td><img src="/s开发者_如何学Ctatic/images/stock/gtk-ok.png" align="left" height="16" width="16"></td>
<td nowrap="nowrap">Starte Upgrade</td>
</tr>
</tbody>
</table>
</button>
Edit:
I tried it this way:
ie.button(:onclick, /start/).click
But it seems onclick is not supported:
> 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
One of these could work (not tested):
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
More information:
- http://wiki.openqa.org/display/WTR/Buttons
- http://wiki.openqa.org/display/WTR/XPath
- http://wiki.openqa.org/display/WTR/Tutorial
I've run into problems like this. If the onclick attribute is not supported, you can do something like this.
b.
buttons.
to_a.
select{|but| but.attribute_value(:onclick).match(/start/) }.
first.
click
This is just something I'm typing out from memory, so it might need some tweaking. But it allows you to match things that Watir doesn't necessarily support.
I think there's also some way to do XPath queries with Watir, you might want to look into that as well
精彩评论