如何使用Selenium WebDriver与Java向下滚动

2022-09-03 10:13:38

我想向下滚动我的网页,我正在使用此代码滚动页面,但它不起作用

public ViewBasketSentToMePageObject viewSlideShare() throws InterruptedException {

    Thread.sleep(500l);

    Actions action1 =new Actions(getDriver());
    action1.keyDown(Keys.CONTROL).sendKeys(String.valueOf('\u0030')).build().perform();

    List<WebElement> function = getDriver().findElements(By.xpath("//a [@ng-click='onItemClick()']"));
    function.get(13).findElement(By.xpath("//img [@ng-src='resources/images/slideshare-icon-small.png']")).click();

    return getFactory().create(ViewBasketSentToMePageObject.class);
}

寻求帮助


答案 1

尝试在下面使用简单的java脚本,您可以滚动页面。

JavascriptExecutor jsx = (JavascriptExecutor)driver;
jsx.executeScript("window.scrollBy(0,450)", "");

答案 2

对于向下滚动:

WebDriver driver = new FirefoxDriver();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("scroll(0, 250);");

或者,您可以执行以下操作:

jse.executeScript("window.scrollBy(0,250)", "");

推荐