我想滚动页面

时间:2014-01-10 07:02:00

标签: selenium selenium-webdriver

我想滚动页面iI使用下面的代码,但我需要手动移动滚动条到1/4然后它会自动滚动这是什么原因,任何新的代码建议。

Actions dragger = new Actions(driver);
    WebElement draggablePartOfScrollbar = driver.findElement(By.className("mCSB_dragger_bar"));
    int numberOfPixelsToDragTheScrollbarDown = 1000;
    dragger.moveToElement(draggablePartOfScrollbar).clickAndHold().moveByOffset(10,numberOfPixelsToDragTheScrollbarDown).release().perform();
    dragger.moveToElement(draggablePartOfScrollbar).clickAndHold().moveByOffset(10,numberOfPixelsToDragTheScrollbarDown).release().perform();

1 个答案:

答案 0 :(得分:0)

我通常使用JavascriptExecutor来实现此目的。

WebDriver driver = new FirefoxDriver();
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("window.scrollTo(0,Math.max(document.documentElement.scrollHeight," + "document.body.scrollHeight,document.documentElement.clientHeight));");