【问题标题】:How to scroll an overflowing div inside a web page如何在网页内滚动溢出的 div
【发布时间】:2016-02-01 20:21:02
【问题描述】:

有没有办法在网页中滚动 div 的滚动条?更准确地说,我正在尝试自动滚动 Instagram 帖子的上下滚动,例如 https://instagram.com/p/9D5Ir3CY3D/?taken-by=bentomonsters。但是,由于滚动条可能使用 CSS 属性隐藏,我可能无法检测到它,因为滚动条不是元素。

下面是滚动条的 css 样式(来自 firebug)。

.-cx-PRIVATE-PostInfo__comments {
    margin-left: -24px;
    margin-right: -24px;
    margin-top: -5px;
    padding-left: 24px;
    padding-right: 24px;
    padding-top: 5px;
}
.-cx-PRIVATE-PostInfo__commentsSidebarVariant {
    overflow: auto;
    padding-bottom: 20px;
} 
.-cx-PRIVATE-PostInfo__comments {
    flex-grow: 1;
}
ol, ul {
    list-style: outside none none;
}
html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, 
blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, 
img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, 
center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, 
tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, figure, 
figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, 
time, mark, audio, video {
    border: 0 none;
    font: inherit;
    margin: 0;
    padding: 0;
    vertical-align: baseline;
}

我尝试过诸如

之类的方法
WebElement commentscroll = dr.findElement(By.className("commentsSidebarVariant"));

jse.executeScript("return arguments[0].scrollTop;", commentscroll);

jse.executeScript("$(\"#commentsSidebarVariant\").animate({ scrollTop: \"100px\" })");  

jse.executeScript("arguments[0].scrollTop = arguments[1];", commentscroll);

WebElement commentscroll = dr.findElement(By.cssSelector(".-cx-PRIVATE-PostInfo__commentsSidebarVariant"));               

jse.executeScript("arguments[0].scrollTop;", commentscroll);

它们都不起作用,因为滚动条甚至没有移动。

【问题讨论】:

标签: java css selenium scrollbar overflow


【解决方案1】:

我使用以下方法使用纯 JS 滚动溢出元素并检查是否达到滚动结束。 当到达滚动结束时,您需要使用类似的方法向上滚动,因为您可能已经从任何位置开始滚动。

以下方法为参考代码,并非全部解决方案:

    // Returns true if scroll succeeded, false otherwise
    private boolean scrollDownOverflowElement(WebElement element, int lazyLoadingGracePeriodMillis) {
        long scrollTopBefore = (long) javascriptExecutor.executeScript("return arguments[0].scrollTop", element);
        javascriptExecutor.executeScript("arguments[0].scrollTop = arguments[0].scrollTop + arguments[0].clientHeight", element);
        if (isAtEndOfScroll(element)) {
            // Wait for 1 second to give additional async results a chance to load (e.g. select2 drop downs)
            sleep(lazyLoadingGracePeriodMillis);
        }
        long scrollTopAfter = (long) javascriptExecutor.executeScript("return arguments[0].scrollTop", element);
        return scrollTopAfter != scrollTopBefore;
    }


    // Returns true if element is at end of scroll, false otherwise
    // See https://developer.mozilla.org/en-US/docs/Web/API/Element.scrollHeight
    private boolean isAtEndOfScroll(WebElement element) {
        // scrollTop is the number of number of pixels scrolled down from the top (0 when scrolled all the way up)
        // scrollHeight is the height of the scroll view of an element. It includes the element padding but not its margin.
        // clientHeight is the inner height of an element in pixels, including padding but not the horizontal scrollbar height, border, or margin. (the displayed size of an overflow element)
        final boolean isAtEnd = (boolean) javascriptExecutor.executeScript("return arguments[0].scrollHeight - arguments[0].scrollTop === arguments[0].clientHeight", element);
        return isAtEnd;
    }

【讨论】:

  • 对不起,我不明白您的参考代码将如何帮助解决我的问题,因为它会检查元素是否已到达滚动底部,但正如我所提到的,我无法知道滚动条元素,因为它是一个 CSS 属性,只有在文本框“填充”时才会出现。
  • 解决方案通过 Javascript 滚动,而不是滚动条。根据元素属性,无论滚动条是否出现,它都会起作用
  • 我尝试在main方法中以scrollDownOverflowElement(commentscroll);这种方式实现scrollDownOverflowElement(WebElement),但是非静态方法scrollDownOverflowElement(WebElement)不能从静态引用上下文。这不是调用方法的正确方式吗?
  • 您不能从静态上下文中调用实例方法。只要不使用实例成员,您就可以将 scrollDownOverflowElement 更改为静态。您将需要提供 javascriptExecutor 作为方法参数,并将此调用的下游方法也更改为静态方法。您还需要将 sleep 更改为 Thread.sleep。
  • 如果要问的不是太多,您能否评论一下参考代码,因为我很难理解每一行应该做什么。谢谢。 @eitan
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多