我已经在下面指定的站点上使用水平和垂直滚动测试了您的场景。
测试网址 « https://trello.com/b/LakLkQBW/jsfiddle-roadmap
;
元素内垂直滚动 «
硒java:
WebElement element = driver.findElement(By.xpath("//div[@id='board']/div[1]/div[1]/div[2]") );
for (int i = 0; i < 5; i++) {
jse.executeScript("arguments[0].scrollTop += 200;", element);
}
javascript |
jquery:
var objDiv = $x("//div[@id='board']/div[1]/div[1]/div[2]")[0];
console.log( objDiv );
objDiv.scrollTop += 100;
//objDiv.scrollTop = objDiv.scrollHeight;
元素内横滚 «
滚动直到元素出现。
滚动到页面末尾。
public void scrollPage() throws InterruptedException {
driver.get("https://stackoverflow.com/q/33094727/5081877");
Actions actions = new Actions(driver);
WebElement element = driver.findElement(By.xpath("//body") );
Actions scrollDown = actions.moveToElement( element );
scrollDown.keyDown(Keys.CONTROL).sendKeys(Keys.END).build().perform();
//jse.executeScript("window.scrollTo(0, document.body.scrollHeight)");
Thread.sleep( 1000 * 4 );
/*Actions scrollUP = actions.moveToElement( element );
scrollUP.keyDown(Keys.CONTROL).sendKeys(Keys.HOME).build().perform();*/
jse.executeScript("window.scrollTo(0, -document.body.scrollHeight)");
scroll_Till_Element( "answer-33142037" );
}
对于 javascript,您可以使用其中任何一种。
window.scrollBy(0,800);
window.scrollTo(0, -document.body.scrollHeight);
scroll(0, -document.body.scrollHeight);
@看