滚动并将slickgrid表读入内存

时间:2014-07-17 18:46:51

标签: ruby selenium-webdriver watir-webdriver slickgrid

我有一个slickgrid表,我试图使用watir-webdriver读入内存。因为在没有向下滚动的情况下通常无法看到完整数据,所以我想创建一个可以滚动表格的函数,并且还能够计算所有行的计数以及访问可能隐藏或不隐藏的任何行。它。这是我到目前为止所做的:

class SlickGridTable

def initialize(element)
                @element = element
end
        ...

        def scroll_down
                location_y = 23
                while true
                        location_y += 1
                        $browser.execute_script("arguments[0].scrollBy(0, #{location_y});", @element)
                end 
        end 
end

但是我经常收到此错误:

Selenium::WebDriver::Error::UnknownError: unknown error: undefined is not a function

1 个答案:

答案 0 :(得分:2)

我也在使用slickgrid,并考虑了类似的方法。相反,我使用scroll_until_present方法扩展了Watir :: Div类。现在我们可以滚动到现在,然后使用网格中的数据。实现这一点后,我没有必要收集所有数据。计算行并不能解决您的问题,但确实有助于找到您希望看到的记录。

# Extends the Watir::Div class to support slick grids
module Watir
  class Div
    # scrolls until it finds the item you are looking for
    # can be used like wait_until_present 
    def scroll_until_present
        scroll_height = browser.execute_script('return document.getElementsByClassName("slick-viewport")[0].scrollHeight')
        (0..scroll_height).step(20).each { |item| 
            browser.execute_script('document.getElementsByClassName("slick-viewport")[0].scrollTop = ' + item.to_s) 
            if present?
                # scroll a little more once the record is found
                item += 30
                browser.execute_script('document.getElementsByClassName("slick-viewport")[0].scrollTop = ' + item.to_s) 
                break
            end 
        }
    end
  end
end