无法使用watir单击html元素

时间:2015-04-02 22:51:32

标签: ruby selenium-webdriver watir watir-webdriver

我想点击"按钮"下面,但无法点击它。

enter image description here

html是:

<table onclick="filtersJob_intrinsic_extender.addRow();return false;" class="FilterList_addLink">
<tbody>
<tr>
<td class="icon"><img src="/commander/lib/images/icn12px_add.gif" alt="Add Filter"></td>
<td class="text">Add Intrinsic Filter</td>
</tr>
</tbody>
</table>

我基本上是用watir和watir-webdriver做的:

browser.td(:text => 'Add Intrinsic Filter').click

我在另一个网站上的另一个类似按钮上尝试了这个方法并且它有效。我想知道为什么它在这里不起作用。

它导致异常:

Selenium::WebDriver::Error::ElementNotVisibleError in 'your rspec spec code'
Element is not currently visible and so may not be interacted with[remote server] file:///C:/Users/john/AppData/Local/Temp/webdriver-profile20150402-8208-15kr0zm/extensions/fxdriver@googlecode.com/components/command_processor.js:7736:in `fxdriver.preconditions.visible'
[remote server] file:///C:/Users/john/AppData/Local/Temp/webdriver-profile20150402-8208-15kr0zm/extensions/fxdriver@googlecode.com/components/command_processor.js:10437:in `DelayedCommand.prototype.checkPreconditions_'
[remote server] file:///C:/Users/john/AppData/Local/Temp/webdriver-profile20150402-8208-15kr0zm/extensions/fxdriver@googlecode.com/components/command_processor.js:10456:in `DelayedCommand.prototype.executeInternal_/h'
[remote server] file:///C:/Users/john/AppData/Local/Temp/webdriver-profile20150402-8208-15kr0zm/extensions/fxdriver@googlecode.com/components/command_processor.js:10461:in `DelayedCommand.prototype.executeInternal_'
[remote server] file:///C:/Users/john/AppData/Local/Temp/webdriver-profile20150402-8208-15kr0zm/extensions/fxdriver@googlecode.com/components/command_processor.js:10401:in `DelayedCommand.prototype.execute/<'
C:/ruby/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/response.rb:51:in `assert_ok'
C:/ruby/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/response.rb:15:in `initialize'
C:/ruby/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/http/common.rb:59:in `new'
C:/ruby/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/http/common.rb:59:in `create_response'
C:/ruby/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/http/default.rb:66:in `request'
C:/ruby/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/http/common.rb:40:in `call'
C:/ruby/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/bridge.rb:629:in `raw_execute'
C:/ruby/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/bridge.rb:607:in `execute'
C:/ruby/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/bridge.rb:364:in `clickElement'
C:/ruby/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/common/element.rb:54:in `click'

1 个答案:

答案 0 :(得分:1)

<强>问题

从评论中我们看到实际上有51个td元素有“Add Intrinsic Filter”文本:

browser.tds(:text => 'Add Intrinsic Filter').length
#=> 51

我们也看到其中一些单元格是可见的而有些则不是 - 即在为每个单元格调用.visible?时,某些单元格返回true而其他单元格返回false:

browser.tds(:text => 'Add Intrinsic Filter').map(&:visible?).uniq
#=> [false, true]

当找到单个元素时,Watir将选择匹配的第一个元素。在这种情况下,我们可以推断出第一个带有文本“Add Intrinsic Filter”的单元格是不可见的: * Selenium :: WebDriver :: Error :: ElementNotVisibleError异常是不可见的。 *根据browser.tds(:text => 'Add Intrinsic Filter').map(&:visible?).uniq结果的排序,第一个元素不可见。

如果没有看到该页面,我们只能假设第一个匹配的单元格不是您实际想要点击的单元格。

<强>解决方案

您需要确定51个单元格中的哪一个实际上是您要单击的单元格,然后使用更具体的定位器。

根据Selenium IDE的结果,你可以这样做:

browser.td(:xpath => '//tr[@id="filtersJob_intrinsic_container"]/td[2]/table[2]/tbody/tr/td[2]').click

指定元素的整个路径可能很容易发生变化,因此您可能希望尝试一些不那么具体的东西。也许尝试找到具有特定id的行,然后查找具有特定文本的单元格:

browser.tr(:id => 'filtersJob_intrinsic_container').td(:class => 'text', :text => 'Add Intrinsic Filter').click

请注意,:class被添加为定位器以尝试获取内部td而不是外部td。