尝试向下滚动页面时出现JavascriptExecutor错误

时间:2017-12-14 09:57:07

标签: javascript selenium-webdriver

以下是片段和获取错误,如下所示:

The method executeScript(String, Object[]) in the type JavascriptExecutor is not applicable for the arguments (String)

代码段:

public class ScrollPage {

    public static void main(String[] args) {

        System.setProperty("webdriver.chrome.driver", "C:\\SeleniumWorkSpace\\chromeDriver\\chromedriver.exe");

        WebDriver driver = new ChromeDriver();

        System.out.println(driver);

        driver.get("https://en.wikipedia.org/wiki/Main_Page");

        driver.manage().window().maximize();

        JavascriptExecutor js = (JavascriptExecutor)driver;

        js.executeScript("scroll(0,2500)");

    }
}

我该如何解决这个问题?

4 个答案:

答案 0 :(得分:0)

尝试不使用

使用它
driver.executeScript("scroll(0,2500)");

答案 1 :(得分:0)

将代码行编辑为:

//import
import org.openqa.selenium.JavascriptExecutor;
//cast driver instance to JavascriptExecutor
JavascriptExecutor js = (JavascriptExecutor)driver;
//invove executeScript method    
js.executeScript("scroll(0, 2500)");
  

注意:在坐标

之间加一个空格

答案 2 :(得分:0)

试试这段代码:

JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("window.scrollTo(0,2500);");

答案 3 :(得分:0)

请尝试以下操作-我在JavascriptExecutor中遇到相同的错误,未正确执行向下滚动。

public WebElement pollingScroll(By locator) {
    WebDriverWait shortWait = new WebDriverWait(driver, 1);
    WebElement element = null;
    while(element == null) {
        ((JavascriptExecutor) driver).executeScript("window.scrollBy(0, 555);");
        try {
            element = 
            shortWait.until(ExpectedConditions.elementToBeClickable(locator));
        } catch(Exception e) {}
    }
    return element;
}