1.下载谷歌(https://www.google.cn/chrome/index.html)及谷歌驱动(https://googlechromelabs.github.io/chrome-for-testing/)
public static void main(String[] args) throws Exception {
/**
* 1、打开窗口
*/
//设置谷歌驱动地址
System.setProperty("webdriver.chrome.driver", "C:\\Program Files/Google/Chrome/Application/chromedriver.exe");
ChromeOptions options = new ChromeOptions();
//允许所有请求
options.addArguments("--remote-allow-origins=*");
WebDriver webDriver = new ChromeDriver(options);
//打开裁决文书网
webDriver.get("https://wenshu.court.gov.cn/website/wenshu/181029CR4M5A62CH/index.html?");
//浏览器最大化
webDriver.manage().window().maximize();
/**
* 2、跳转登录
*/
// 根据XPath定位登录按钮并单击
// XPATH的获取可以用chrome的F12开发者模式中Element-右键-copy-copy xpath来获取
webDriver.findElement(By.xpath("//*[@id=\"loginLi\"]/a")).click();
webDriver.getPageSource();
System.out.println(new Date());
//强制等待30秒用于输入验证码及跳转登录页面等待页面渲染
Thread.sleep(30000);
//进入登录框的iframe
webDriver.switchTo().frame(webDriver.findElements(By.tagName("iframe")).get(0));
System.out.println(new Date());
//强制等待1秒用于登录框的iframe渲染
Thread.sleep(1000);
//模拟键入手机号
webDriver.findElement(By.xpath("//*[@id=\"root\"]/div/form/div/div[1]/div/div/div/input") ).sendKeys("你的手机");
Thread.sleep(1000);
//模拟键入密码
webDriver.findElement(By.xpath("//*[@id=\"root\"]/div/form/div/div[2]/div/div/div/input") ).sendKeys("你的密码");
Thread.sleep(1000);
//点击登录
webDriver.findElement(By.xpath("//*[@id=\"root\"]/div/form/div/div[3]/span")).click();
Thread.sleep(3000);
//必须加上表单退出,否者就是死元素无法定位
webDriver.switchTo().defaultContent();
/**
* 根据类型点击查询
*/
//定义文书类型:"刑事案件","民事案件","行政案件","赔偿案件","执行案件","其他案件"
String[] wenshuTypeArr={"刑事案件","民事案件","行政案件","赔偿案件","执行案件","其他案件"};
//根据类型便利点击查询
for(String wenshuType:wenshuTypeArr){
Thread.sleep(5000);
//点击类型文本链接
webDriver.findElement(By.linkText(wenshuType)).click();
Thread.sleep(10000);
webDriver.switchTo().window(webDriver.getWindowHandles().toArray()[webDriver.getWindowHandles().toArray().length-1].toString());
//将每页文件数设置为最大,15条
new Select(webDriver.findElement(By.xpath("//*[@id=\"_view_1545184311000\"]/div[8]/div/select"))).selectByVisibleText("15");
/**
* 这里可以加关键字/案由等点击循环,下边代码则写入其内
*/
//最多显示600条文件,也就是40页
for(int i=0;i<40;i++){
try{
Thread.sleep((5000+(i*1000))/10);
//每页15条
for(int j=0;j<15;j++){
try{
Thread.sleep((5000+(j*1000))/10);
String xpath="//*[@id=\"_view_1545184311000\"]/div["+(j+3)+"]/div[6]/div/a[2]";
//判断点击页码是否成功
boolean isSuccess=true;
try{
webDriver.findElement(By.xpath(xpath));
isSuccess=true;
}catch (Exception e){
isSuccess=false;
}
if(isSuccess){
webDriver.findElement(By.xpath(xpath)).click();
}else{
xpath="//*[@id=\"_view_1545184311000\"]/div["+(j+3)+"]/div[5]/div/a[2]";
try{
webDriver.findElement(By.xpath(xpath));
isSuccess=true;
}catch (Exception e){
isSuccess=false;
}
if(isSuccess){
webDriver.findElement(By.xpath(xpath)).click();
}
}
}catch (Exception e){
System.out.println("pageSize:"+(j+1));
}
}
// 下一页按钮,不能用Xpath定位,因为“下一页”按钮位置不固定
Thread.sleep(5000);
webDriver.findElement(By.linkText("下一页")).click();
//必须加上表单退出,否者就是死元素无法定位
webDriver.switchTo().defaultContent();
}catch (Exception e){
System.out.println("pageIndex:"+(i+1));
}
}
}
//关闭整个浏览器窗口并终止与浏览器的会话
webDriver.quit();
}