下载谷歌浏览器驱动 /index.html 创建maven项目 <!--?xml versi="" encoding=""UTF-8"?"--><!--?xml--> <project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance" xsi:schemaLocati> <modelversion>4.0.0</modelversion> <groupid>org.example</groupid> <artifactid>selenium-demo</artifactid> <version>1.0-SNAPSHOT</version> <dependencies></dependencies> <dependency></dependency> <groupid>org.seleniumhq.selenium</groupid> <artifactid>selenium-java</artifactid> <version>3.141.59</version> <repositories></repositories> <repository></repository> <id>aliyun</id> <name>aliyun nexus</name> <url>/repository/public</url> <releases></releases> <enabled>true</enabled> <pluginrepositories></pluginrepositories> <pluginrepository></pluginrepository> <id>aliyun</id> <name>aliyun nexus</name> <url>/repository/public</url> <releases></releases> <enabled>true</enabled> 编写代码 public class Main { public static void main(String[] args) { //配置谷歌浏览器驱动位置 System.setProperty("webdriver.chrome.driver", "D:chromedriver.exe"); while (true) { // 初始化一个chrome浏览器实例,实例名称叫driver WebDriver driver = new ChromeDriver(); //窗口最大化 driver.manage().window().maximize(); // get()打开一个站点,文章列表页 driver.get("/lht931942788"); //通过每篇文章标题的样式类名,获取标题、概要List List<webelement>elements = driver.findElements(By.className("article-item-box"));</webelement> //遍历文章列表 for (WebElement element : elements) { //获取A标签 WebElement a = element.findElement(By.tagName("a")); //触发点击事件,访问文章 a.click(); } // 关闭并退出浏览器 driver.quit(); } } }