0

I am trying to scroll to an element using javascript executor on my Ruby script using selenium. However, the script scrolls all the way down of the page and skips the element. Thus not interacting with the element.

Scroll method:

def scroll_into_view(element, locator=nil)
    element = element.nil? ? find(locator) : element
    @wd.execute_script('arguments[0].scrollIntoView(true);', element)
  end

Reference:

scroll_into_view(nil, gear_icon_span_locator)

Do you know of any other ways to scroll to an element in ruby?

I've tried action builders and actually moving to an element, that didn't seem to work.

Shahboz
  • 466
  • 2
  • 10
  • 30
  • I use 'arguments[0].scrollIntoView();' without passing the true parameter and it scrolls right to the element. Have you tried that? – RKelley Mar 28 '19 at 18:51
  • Tried it, it is scrolling all the way to the bottom of the page and the element is skipped. – Shahboz Mar 28 '19 at 18:54

2 Answers2

0

In watir you can scroll to element using the below.

element.scroll.to :bottom
supputuri
  • 13,644
  • 2
  • 21
  • 39
0

The script that worked is this, I found the answer from here: Scroll that works for ruby

  def scroll_to_element(element, locator=nil)
    script_string = "var viewPortHeight = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);" +
        "var elementTop = arguments[0].getBoundingClientRect().top;" +
        "window.scrollBy(0, elementTop-(viewPortHeight/2));"
    element = element.nil? ? find(locator) : element
    execute_script(script_string, element)
  end
Shahboz
  • 466
  • 2
  • 10
  • 30