1

I'm using watir-webdriver gem and recently came to a halt when trying to click some elements on firefox.

When trying to click an element that is out of viewport, the page scrolls and places the element at the top of the page. Which is essentially just:

require 'watir-scroll'
element = @browser.div({:id=>'potatoes'})
element.wait_until_present
@browser.scroll.to(element)
element.click

OR

$("div[id='potatoes']").scrollIntoView();

However this is causing me some issues as I have an overlaying banner running at the top of the page, which in turn causes the element to be "behind" it. Is there an easy way to attempt to scroll to an element, but instead of placing it at the top of the viewport, try to centre it.

Additionally I have used:

$("div[id='potatoes']").scrollIntoView(false);

to scroll to the element and place it at the bottom of the page, however it would be nice to have it centred, in case I wanted to have banners at the bottom of the page.

Regards

Mantas
  • 3,179
  • 4
  • 20
  • 32
  • 1
    see http://stackoverflow.com/questions/36706986/watir-webdriver-throws-not-clickable-error-even-when-element-is-visible-prese/36708613#36708613 – Carldmitch May 24 '16 at 15:21

1 Answers1

0

IE & Firefox implement elementScrollBehavior. It is set for all elements for the entire session of the driver. Where 0 is default and places element at the top of the view port, and 1 scrolls the element to the bottom of the view port.

caps = Selenium::WebDriver::Remote::Capabilities.firefox('elementScrollBehavior' => 1)
Watir::Browser.new :firefox, desired_capabilities: caps

For more control you would need to use javascript (Watir should probably investigate more direct support for scrolling to an offset of an element).

browser.execute_script('javascript:window.scrollBy(250,350)')
titusfortner
  • 4,099
  • 2
  • 15
  • 29