简体   繁体   English

如何在 Java 中使用 Selenium WebDriver 向下滚动

[英]How to scroll down using Selenium WebDriver with Java

I want to scroll down my web page and im using this code to scroll page but it is not working我想向下滚动我的网页,我使用此代码滚动页面但它不起作用

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);
}

Looking for help寻求帮助

Try using simple java script below and you can scroll the page.尝试使用下面的简单 java 脚本,您可以滚动页面。

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

For scroll down:向下滚动:

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

or, you can do as follows:或者,您可以执行以下操作:

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

Scroll until find the WebElement滚动直到找到WebElement

Try this:试试这个:

((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", your_WebElement);
WebElement element = driver.findElement(By.xpath("//input [@id='giveid']"));

((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", element);

Use this.用这个。 This will help you to scroll down at the particular element.这将帮助您向下滚动特定元素。 I had tested on my website even.我什至在我的网站上进行了测试。 It is working fine.它工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM