package com.boyuan.selenium;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class SeleniumDemo7 {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "D:/tool/chromedriver.exe");
WebDriver web=new ChromeDriver();
web.get("http://www.csrc.gov.cn/pub/newsite/ztzl/zcjd/");
web.manage().window().maximize();
web.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement msg= web.findElement(By.xpath("/html/body/div/div/div[5]/div[2]/div[1]/div[2]/ul"));
List<WebElement> rows = msg.findElements(By.tagName("li"));
int num=rows.size();
for(int i=1;i<=2;i++){
WebElement msg1=web.findElement(By.xpath("/html/body/div/div/div[5]/div[2]/div[1]/div[2]/ul/li["+i+"]/a"));
String title=msg1.getText();
System.out.println("新闻标题"+i+":"+title);
Thread.sleep(1000);
String linkText=msg1.getAttribute("href");
web.get(linkText);
System.out.println("新闻URL:"+linkText);
WebElement msg2=web.findElement(By.xpath("/html/body/div/div/div[5]/div/div[3]"));
System.out.println("新闻内容"+i+":"+msg2.getText());
Thread.sleep(1000);
web.get("http://www.csrc.gov.cn/pub/newsite/ztzl/zcjd/");
}
web.close();
}
}