无法使用watir单击html元素 [英] Cannot click html element with watir

查看:52
本文介绍了无法使用watir单击html元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想单击下面的按钮",但不能单击它.

I want to click the "button" below, but cannot click it.

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来做到这一点的:

I am basically doing this with watir and watir-webdriver:

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

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

I tried this method on another similar button in another website and it worked. I wonder why it does not work here.

它会导致异常:

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'

推荐答案

问题

从注释中我们可以看到,实际上有51个td元素的文本为添加内部过滤器":

From the comments we see that there are actually 51 td elements that have the text "Add Intrinsic Filter":

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

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

We also see that some of these cells are visible and some are not - ie when calling .visible? for each cell, some returned true while others returned false:

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

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

When locating a single element, Watir will pick the first element that matches. In this case, we can infer that the first cell with text "Add Intrinsic Filter" is not visible: * The Selenium::WebDriver::Error::ElementNotVisibleError exception does it was not visible. * Based on the ordering of the results of browser.tds(:text => 'Add Intrinsic Filter').map(&:visible?).uniq, the first element was not visible.

在没有看到页面的情况下,我们只能假定第一个匹配的单元格不是您实际要单击的单元格.

Without seeing the page, we can only assume that the first matching cell is not the one you actually want to click.

解决方案

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

You need to determine which of the 51 cells is actually the one you want to click and then use a more specific locator.

根据Selenium IDE的结果,您可以执行以下操作:

Based on the result from Selenium IDE, you could do:

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

指定元素的整个路径可能难以更改,因此您可能想尝试一些不太具体的东西.也许尝试查找具有特定ID的行,然后查找具有特定文本的单元格:

Specifying the whole path to the element can be brittle to changes, so you might want to try something a little less specific. Perhaps try locating the row that has a specific id and then the cell with the specific text:

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

请注意,添加了:class作为定位符,以尝试获取内部td而不是外部td.

Note that the :class was added as a locator to try to get the inner td rather than the outer one.

这篇关于无法使用watir单击html元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆