webdriver提供了executeScript()方法
((JavascriptExecutor)webDriver).executeScript("document.documentElement.scrollTop=500");
scrollTop的值是显示下滑距离的,值越大下滑距离就越大
以下是一个例子:
public class baidu2 {
public static void main(String[] args) throws InterruptedException {
System.out.println("开始执行代码");
System.setProperty("webdriver.chrome.driver", "C:\app\chromedriver_win32\chromedriver.exe");
WebDriver webDriver = new ChromeDriver();
webDriver.manage().window().maximize();
webDriver.get("http://www.baidu.com");
Thread.sleep(3000);
webDriver.findElement(By.id("kw")).sendKeys("webdriver api");
webDriver.findElement(By.id("su")).click();
Thread.sleep(3000);
((JavascriptExecutor)webDriver).executeScript("document.documentElement.scrollTop=500");
Thread.sleep(5000);
System.out.println("写完了哈");
webDriver.close();
}
}
网友评论