绕过Element的方法无法滚动到视图中 - 使用Ruby的Watir-webdriver

时间:2013-02-20 00:30:52

标签: ruby webdriver selenium-webdriver watir-webdriver

因此,我们在页面中有以下代码:

<div class="toggle-wrapper">
  <input id="HasRegistration_true" class="registration_required toggle" type="radio" value="True" name="HasRegistration" data-val-required="The HasRegistration field is required." data-val="true">
  <label for="HasRegistration_true" class="">On</label>
  <input id="HasRegistration_false" class="registration_required toggle" type="radio" value="False" name="HasRegistration" checked="checked">
  <label class="checked" for="HasRegistration_false">Off</label>
</div>

这是2个单选按钮。 '开和关'。 '关'是默认值。

使用Watir-webdriver和Ruby,我们要选择'On'单选按钮。我们这样做:

browser.radio(:id => "HasRegistration_true").set

但是这样做会导致以下错误:

`WebElement.clickElement': Element cannot be scrolled into view:[object HTMLInputElement] (Selenium::WebDriver::Error::MoveTargetOutOfBoundsError)

我们知道Selenium 2将页面滚动到元素,因此尝试向下滚动是没用的。 我们一直在使用最新版本的watir-webdriver和ruby。

我们无法更改页面的HTML,因为我们是QA工程师。

5 个答案:

答案 0 :(得分:1)

以下是两个对我有用的解决方案:

有一个名为watir-scroll的Ruby gem可以工作。

然后

require 'watir-scroll'
browser.scroll.to browser.radio(:id, "HasRegistration_true")

如果你不想添加宝石,我的同事提出了一些令人惊讶的(对我而言)与上述代码具有相同的效果:

browser.radio(:id, "HasRegistration_true").focus
browser.radio(:id, "HasRegistration_true").set

在我的代码中,&#34; .focus&#34;直接滚动到以前不可见的元素。它也可能适合你。

答案 1 :(得分:0)

首先尝试使用XPATH定位元素:

browser.element(:xpath, "//input[@id='HasRegistration_true']").click

或者,如果它是您尝试定位的隐藏元素,那么最好使用CSS。下载firefox的firebug附加组件并复制元素的CSS路径。

应该是这样的:

browser.element(:css => "the CSS path you have copied from Firebug").click

其中一个应该为你做!!

祝你好运!

答案 2 :(得分:0)

你可以通过执行一些javascript来动态操作html,使无线电元素可以设置。要在页面上执行javascript,请执行以下操作: @browser.execute_script("your javascript here")

我使用类似以下javascript的东西从标签标签中删除了一个标签,这标签​​将其移出我试图针对我遇到的Chrome特定问题的输入标签。 execute_script("$(\"label.classname\").removeClass(\"classname inline\")")

答案 3 :(得分:0)

如果元素包含在表单和div(Wrap)类中,我发现我必须执行以下操作才能单击“ {{”上的“”单选按钮3}}?“页面:

div_list = @ browser.form(:action =&gt;“AboutYourVehicle.aspx?ton_t = CTMMO&amp; prdcls = PC&amp; rqstyp = newmotorquote&amp; AFFCLIE = CM01”)。div(:class =&gt;“inputWrap “)。divs(:class =&gt;”custom-radio“)

然后: 的 div_list [1]。点击

希望这也解决了你的问题: - )

答案 4 :(得分:0)

我没有使用watir,但我有同样的错误“......无法滚动到视图中......”。我试图使用watir来解决它并且不适合我。然后,我使用ActionBuilder( move_to 点击),错误消失。

我要避免错误的一行是:

@driver.action.move_to(*webelement*).click.perform

我希望它对你有用