硒不向下滚动

时间:2019-12-18 15:06:25

标签: selenium-webdriver selenium-chromedriver

我的硒有问题。 我想向下滚动页面,并创建了此方法:

public static void scrollDownToEndOfPage()
    {
        By locator;
        locator = By.xpath("//label[contains(text(),'Blocked advertisers')]//ancestor::nz-form-item//nz-select//input");
        WebDriver driver2 = WebDriverMgr.getDriver();
        JavascriptExecutor js = (JavascriptExecutor) driver2;
        js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
        ((JavascriptExecutor)driver2).executeScript("window.scrollTo(0, document.body.scrollHeight)");
        js.executeScript("window.scrollBy(0,250)");
        js.executeScript("arguments[0].scrollIntoView();", locator);
    }

当我调试没有任何反应时,这是日志

Wed Dec 18 18:35:14 IST 2019:INFO: WebDriver: Script - window.scrollTo(0, document.body.scrollHeight)
Wed Dec 18 18:35:14 IST 2019:INFO: WebDriver: Script - window.scrollTo(0, document.body.scrollHeight)
Wed Dec 18 18:35:14 IST 2019:INFO: WebDriver: Script - window.scrollBy(0,250)
Wed Dec 18 18:35:14 IST 2019:ERROR: Argument is of an illegal type: org.openqa.selenium.By$ByXPath
Wed Dec 18 18:35:14 IST 2019:INFO: WebDriver: Script - arguments[0].scrollIntoView();


java.lang.IllegalArgumentException: Argument is of an illegal type: org.openqa.selenium.By$ByXPath

    at org.openqa.selenium.remote.internal.WebElementToJsonConverter.apply(WebElementToJsonConverter.java:83)
    at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
    at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
    at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)

这是我运行滚动条后的屏幕 enter image description here

页面是从屏幕侧面打开的绘图,我想向下滚动它,有人可以建议吗?

解决方案如Sooraj的建议

 public static void scrollToElement(By locator)
    {

      //  locator = By.xpath("//label[contains(text(),'Blocked advertisers')]//ancestor::nz-form-item//nz-select//input");
        WebDriver driver2 = WebDriverMgr.getDriver();
        WebElement element = driver2.findElement(locator);
        JavascriptExecutor js = (JavascriptExecutor) driver2;
       // js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
      //  ((JavascriptExecutor)driver2).executeScript("window.scrollTo(0, document.body.scrollHeight)");
      //  js.executeScript("window.scrollBy(0,250)");
        js.executeScript("arguments[0].scrollIntoView();", element);
    }

enter image description here

1 个答案:

答案 0 :(得分:1)

尝试使用javascript scrollIntoView并在不可见区域传递要与之交互的元素:

JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement element = driver.findElement(locator);
js.executeScript("arguments[0].scrollIntoView();", element);