在使用selenium的过程中,我们会遇到打开一个页面后,等待进度条加载完成,再去滚动鼠标或者下拉页面的时候,页面还会继续加载内容,这就会导致在定位元素并且滑动的时候,代码报错:元素不存在。
这是因为页面需要手动下拉或者向下滑动,才能加载全部(剩下的)的内容。可以通过sendKeys(Keys.DOWN)方法多下拉几次,来实现页面全部内容的加载。
下面示例是以百度新闻为例:
package com.keydom;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class selenium2 {
public static WebDriver driver;
public static void main(String[] args) throws Exception {
System.setProperty("webdriver.chrome.driver", "E:\\ecl