代码之家  ›  专栏  ›  技术社区  ›  switch201

处理“内部”滚动条时如何滚动到视图中

  •  1
  • switch201  · 技术社区  · 7 年前

    如果这个问题有点模棱两可,我很抱歉。我注意到,如果Geb元素不在页面上,并且需要使用“内部”滚动条滚动,那么Geb元素就不会被滚动到视图中

    所谓“内部”滚动条,我指的是嵌套在给定网页中的滚动条,与全局网页的滚动条分离。

    我做过一些不同的手工滚动这些内部滚动条的方法,但是我想知道Geb是否提供了处理这些嵌套滚动条的功能。

    下面是一个代码片段,演示如何处理查找给定行的操作:

    class TabledModule extends Module {
        static content = {
            headers {$(By.xpath("//lane-group-header"))}
            table {$(By.xpath("//div[@class=',y-class']"))}
        }
    
        Navigator getAllRows(){
            return table.children()
        }
    
        Navigator getRow(String text){
            return table.children().find{it.text().contains(text)}
        }
    
        Navigator getRow(int index){
            return table.children()[index]
        }
    }
    

    根据我的剧本:

    getAllRows() //returns 50 which it should (only 20 are displayed)
    def row = getRow(45) //returns a navigator as it should
    row.click() //successfully clicks the correct row
    def row2 = getRow("someString") //returns null when the row is off the page this is the problem and I'm wondering now if it is a bug, since getting the row by index seems to work fine.
    

    对于这个模块,只显示50行中的20行来显示其他行,您必须滚动嵌套的滚动条才能找到它们。我要访问的行在列表的下方,因此需要滚动才能访问它。

    有趣的是 getAllRows().size() 返回正确的行数:50,但是当我调用 getRow

    1 回复  |  直到 7 年前
        1
  •  0
  •   switch201    7 年前

    所以我发现了我的问题所在。如果我用索引而不是字符串从屏幕上抓取一个元素。Geb能够抓取导航器并能够单击所述导航器,但是如果元素不在屏幕上,那么Geb就无法获取元素上的文本。为了解决这个问题,我实现了这个方法。

    Navigator getRow(String text){
        JavascriptExecutor jse = (JavascriptExecutor)browser.driver
        for(int x = 0; x<getAllRows().size();x++){
            def row = getRow(x)
            WebElement element = row.firstElement()
            jse.executeScript("arguments[0].scrollIntoView(true);", element);
            if(row.text().contains(text)){
                return row
            }
        }
        return null
    }