GetDriverInfo

package test;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.Select;

public class GetDriverInfo {

    public static void main(String[] args) {

        String[] addrs =
            new String[] {"001", "002", "003", "004", "005", "006", "007",
                "008", "011"};
        // <option value="001">公交驾校</option>
        // <option value="002">杭州长运</option>
        // <option value="003">新塘</option>
        // <option value="004">铁路</option>
        // <option value="005">浙公校</option>
        // <option value="006">杭汽校</option>
        // <option value="007">富阳桐江</option>
        // <option value="008">浙江经职</option>
        // <option value="011">交通职高</option>

        Calendar today = Calendar.getInstance();
        long todayTime = today.getTimeInMillis();

        for (int j = 0; j < addrs.length; j++) {
            for (int k = 0; k < 35; k++) {

                long newTime = todayTime + k * 3600 * 24 * 1000;
                Date newDate = new Date(newTime);
                Calendar newCal = Calendar.getInstance();
                newCal.setTimeInMillis(newTime);
                if (newCal.get(Calendar.MONTH) >= 5) {
                    break;
                }

                getContent(addrs[j], newDate);
            }
        }
    }

    private static void getContent(String addr, Date newDate) {
        String url = "http://www.qc5qc.com/xqc/mlpxfind.php";
//        WebDriver driver = new InternetExplorerDriver();
         WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get(url);

        String date = "";
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        date = df.format(newDate);

        Select selectedList = new Select(driver.findElement(By.name("pxd")));
        selectedList.selectByValue(addr);
        System.out.println("===============");
        System.out.println("addr:"
            + selectedList.getFirstSelectedOption().getText());
        System.out.println("date:" + date);

        WebElement dateElm = driver.findElement(By.id("rq"));

        dateElm.clear();

        dateElm.sendKeys(date);

        driver.findElement(By.name("Submit")).click();

        WebElement tbElm =
            driver.findElement(By.id("form1"))
                .findElement(By.xpath((".//table[2]//table")));

        // System.out.println(tbElm.getText());

        for (int i = 2; i <= 5; i++) {
            String xpathStr = ".//tr[" + i + "]/td[" + 3 + "]";
            String oneValue = tbElm.findElement(By.xpath(xpathStr)).getText();
             if(!"0".equals(oneValue)) {
                 System.out.println("oneValue:" + oneValue);
             }
//            System.out.println(oneValue);
        }

        driver.close();
    }
}

你可能感兴趣的:(java)