Scroll Element into View with Selenium

前端 未结 30 2671
时光说笑
时光说笑 2020-11-22 08:31

Is there any way in either Selenium 1.x or 2.x to scroll the browser window so that a particular element identified by an XPath is in view of the browser? There is a focus m

30条回答
  •  耶瑟儿~
    2020-11-22 09:00

    Sometimes I also faced the problem of scrolling with Selenium. So I used javaScriptExecuter to achieve this.

    For scrolling down:

    WebDriver driver = new ChromeDriver();
    JavascriptExecutor js = (JavascriptExecutor)driver;
    js.executeScript("window.scrollBy(0, 250)", "");
    

    Or, also

    js.executeScript("scroll(0, 250);");
    

    For scrolling up:

    js.executeScript("window.scrollBy(0,-250)", "");
    

    Or,

    js.executeScript("scroll(0, -250);");
    

提交回复
热议问题