方法一:利用Selenium里的Actions和Keys模拟键盘操作,从而控制页面滚动。
方法二:利用JavaScript实现页面滚动。
代码如下:
package com.ceres.demos;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
public class scrollTest {
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new ChromeDriver();
driver.get("https://www.baidu.com/s?wd=window");
Thread.sleep(2000);
/*
* 方法一:利用Selenium里的Actions和Keys模拟键盘操作,从而控制页面滚动;
*/
Actions actions = new Actions(driver);
actions.sendKeys(Keys.DOWN).perform();
Thread.sleep(1000);
actions.sendKeys(Keys.ARROW_DOWN).perform();
Thread.sleep(1000);
actions.sendKeys(Keys.PAGE_DO