Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A way around Element cannot be scrolled into view - Watir-webdriver with Ruby

So, we have the following code in our page:

<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>

These are 2 radio buttons. 'On' and 'Off'. 'Off' is the default value.

Using Watir-webdriver and Ruby, we want to select the 'On' radio button. We do so like this:

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

But in doing so, we get the following error:

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

We know Selenium 2 scrolls the page to the element, so trying to scroll down is useless. We are always using the latest releases of watir-webdriver and ruby.

We can't change the HTML of the page since we're QA engineers.

like image 357
bgilhutton Avatar asked Feb 20 '13 00:02

bgilhutton


1 Answers

Here are two solutions that have worked for me:

There is a Ruby gem called watir-scroll that can work.

Then

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

If you don't want to add a gem, my co-worker suggested something that somewhat surprisingly (to me) had the same effect as the above code:

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

In my code, ".focus" scrolled straight to the element that was previously not visible. It may work for you as well.

like image 189
pjd Avatar answered Nov 20 '22 14:11

pjd