1.下載谷歌(https://www.google.cn/chrome/index.html)及谷歌驅(qū)動(dòng)(https://googlechromelabs.github.io/chrome-for-testing/)
public static void main(String[] args) throws Exception {
/**
* 1、打開窗口
*/
//設(shè)置谷歌驅(qū)動(dòng)地址
System.setProperty("webdriver.chrome.driver", "C:\\Program Files/Google/Chrome/Application/chromedriver.exe");
ChromeOptions options = new ChromeOptions();
//允許所有請(qǐng)求
options.addArguments("--remote-allow-origins=*");
WebDriver webDriver = new ChromeDriver(options);
//打開裁決文書網(wǎng)
webDriver.get("https://wenshu.court.gov.cn/website/wenshu/181029CR4M5A62CH/index.html?");
//瀏覽器最大化
webDriver.manage().window().maximize();
/**
* 2、跳轉(zhuǎn)登錄
*/
// 根據(jù)XPath定位登錄按鈕并單擊
// XPATH的獲取可以用chrome的F12開發(fā)者模式中Element-右鍵-copy-copy xpath來獲取
webDriver.findElement(By.xpath("http://*[@id=\"loginLi\"]/a")).click();
webDriver.getPageSource();
System.out.println(new Date());
//強(qiáng)制等待30秒用于輸入驗(yàn)證碼及跳轉(zhuǎn)登錄頁面等待頁面渲染
Thread.sleep(30000);
//進(jìn)入登錄框的iframe
webDriver.switchTo().frame(webDriver.findElements(By.tagName("iframe")).get(0));
System.out.println(new Date());
//強(qiáng)制等待1秒用于登錄框的iframe渲染
Thread.sleep(1000);
//模擬鍵入手機(jī)號(hào)
webDriver.findElement(By.xpath("http://*[@id=\"root\"]/div/form/div/div[1]/div/div/div/input") ).sendKeys("你的手機(jī)");
Thread.sleep(1000);
//模擬鍵入密碼
webDriver.findElement(By.xpath("http://*[@id=\"root\"]/div/form/div/div[2]/div/div/div/input") ).sendKeys("你的密碼");
Thread.sleep(1000);
//點(diǎn)擊登錄
webDriver.findElement(By.xpath("http://*[@id=\"root\"]/div/form/div/div[3]/span")).click();
Thread.sleep(3000);
//必須加上表單退出,否者就是死元素?zé)o法定位
webDriver.switchTo().defaultContent();
/**
* 根據(jù)類型點(diǎn)擊查詢
*/
//定義文書類型:"刑事案件","民事案件","行政案件","賠償案件","執(zhí)行案件","其他案件"
String[] wenshuTypeArr={"刑事案件","民事案件","行政案件","賠償案件","執(zhí)行案件","其他案件"};
//根據(jù)類型便利點(diǎn)擊查詢
for(String wenshuType:wenshuTypeArr){
Thread.sleep(5000);
//點(diǎn)擊類型文本鏈接
webDriver.findElement(By.linkText(wenshuType)).click();
Thread.sleep(10000);
webDriver.switchTo().window(webDriver.getWindowHandles().toArray()[webDriver.getWindowHandles().toArray().length-1].toString());
//將每頁文件數(shù)設(shè)置為最大,15條
new Select(webDriver.findElement(By.xpath("http://*[@id=\"_view_1545184311000\"]/div[8]/div/select"))).selectByVisibleText("15");
/**
* 這里可以加關(guān)鍵字/案由等點(diǎn)擊循環(huán),下邊代碼則寫入其內(nèi)
*/
//最多顯示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="http://*[@id=\"_view_1545184311000\"]/div["+(j+3)+"]/div[6]/div/a[2]";
//判斷點(diǎn)擊頁碼是否成功
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="http://*[@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定位,因?yàn)椤跋乱豁摗卑粹o位置不固定
Thread.sleep(5000);
webDriver.findElement(By.linkText("下一頁")).click();
//必須加上表單退出,否者就是死元素?zé)o法定位
webDriver.switchTo().defaultContent();
}catch (Exception e){
System.out.println("pageIndex:"+(i+1));
}
}
}
//關(guān)閉整個(gè)瀏覽器窗口并終止與瀏覽器的會(huì)話
webDriver.quit();
}