How to scroll a specific DIV using Selenium WebDriver with Java?

前端 未结 10 1081
后悔当初
后悔当初 2020-12-02 18:41

Some of WebElements are not recognized by WebDriver, WebDriver fails to find the element which is not visible in browser\'s visible area.

In order to make the WebEle

10条回答
  •  星月不相逢
    2020-12-02 19:23

    None of the posted answers worked for me, however I have found a solution based on this post.

    ((JavascriptExecutor) driver).executeScript(
        "arguments[0].scrollTop=arguments[1].offsetTop",
        divWithScrollbarElement,
        elementToScrollTo);
    

    where divWithScrollbarElement is the div element which you are looking to scroll, and elementToScrollTo is the child element which you want to make viewable (which in my case was actually the parent of the element which I was initially trying to view). If elementToScrollTo is not actually in the DOM yet, you may need to use the script once to scroll down as far as possible, and then again once more elements have loaded.

提交回复
热议问题