273286078

导航

 

测试环境:http://nvsm.cnki.net/kns/brief/result.aspx?dbprefix=SCDB  

程序功能:对各个文献库的高级检索功能,输入检索条件做检索,提取加密的ksqlvalue,通过工具对ksqlvalue解密生成ksql语句,输出的文本文件

package test.selenium;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

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.support.ui.Select;

public class NvsmTest {

    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        
        List<String> dbprefixs = new ArrayList();    //库名存的集合里
        dbprefixs.add("CJFQ");    //期刊
        dbprefixs.add("CDMD");    //博硕
        dbprefixs.add("CYFD");    //年鉴
        dbprefixs.add("CCND");    //报纸    
        dbprefixs.add("CIPD");    //会议
//        dbprefixs.add("WWBD");    //图书
            
        getKsqlValue(dbprefixs);    //取ksqlvalue值写文件
        jiemd5();        //对ksqlvalue值解密
    }
    
    public static void getKsqlValue(List<String> dbprefixs) throws Exception
    {
        String path01 = "C:\\selenium_ksqlmd5.txt";        //ksqlvalue值写文件
        FileOutputStream fos = new FileOutputStream(path01);
        OutputStreamWriter osw = new OutputStreamWriter(fos);
        BufferedWriter bw = new BufferedWriter(osw);
        
        for(String dbprefix:dbprefixs)        //遍历集合中的库名
        {
            WebDriver driver = new FirefoxDriver();
            String path = "http://nvsm.cnki.net/kns/brief/result.aspx?dbprefix="+dbprefix;
            System.out.println(path);
            driver.get(path);
            driver.manage().window().maximize();
            
            WebElement weselect01 = driver.findElement(By.xpath("//*[@id=\"txt_1_sel\"]"));        //检索条件第一个下拉框
            Select s01 = new Select(weselect01);
            WebElement weselect02 = driver.findElement(By.xpath("//*[@id=\"txt_2_sel\"]"));        //检索条件第二个下拉框
            Select s02 = new Select(weselect02);
            
            List<WebElement> selectlist01 = s01.getOptions();        //检索条件第一个下拉框的元素集合
            List<WebElement> selectlist02 = s02.getOptions();        //检索条件第二个下拉框的元素集合
            
            int num = selectlist01.size();
            int random01 = new Random().nextInt(num);        //检索条件第一个下拉框元素随机取值
            int random02 = new Random().nextInt(num);        //检索条件第二个下拉框元素随机取值
            s01.selectByIndex(random01);
            s02.selectByIndex(random02);
            
            driver.findElement(By.xpath("//*[@id=\"txt_1_value1\"]")).sendKeys("综述");        //检索条件第一个文本框
            driver.findElement(By.xpath("//*[@id=\"txt_2_value1\"]")).sendKeys("经济");        //检索条件第二个文本框
            Thread.sleep(1000);
            WebElement btn = driver.findElement(By.xpath("//input[@id=\"btnSearch\"]"));    //检索按钮
            btn.click();
            Thread.sleep(3000);
            WebElement iframe = driver.findElement(By.xpath("//iframe[@id=\"iframeResult\"]"));        //定位检索结果的frame
            System.out.println(iframe.getAttribute("id"));        //输入frame的id
            driver.switchTo().frame(iframe);    //切换frame
            WebElement ksql = driver.findElement(By.xpath(".//*[@id='SqlValue']"));        //定位sqlvalue元素
            
            String ksqlvalue = ksql.getAttribute("value");        //取SqlValue元素值
            
            bw.write(ksqlvalue);        //SqlValue值写文件
            bw.newLine();
            bw.flush();
            driver.quit();
        }
        bw.close();    
    }
    
    public static void jiemd5()
    {
        WebDriver driver = new FirefoxDriver();
        String tools = "http://192.168.107.188/dec/t.aspx";
        driver.get(tools);
        driver.manage().window().maximize();
            
        String frompath = "C:\\selenium_ksqlmd5.txt";        //读文件
        String topath = "C:\\selenium_ksql.txt";            //写文件
        
        FileInputStream fis;
        FileOutputStream fos;
        try {
            fis = new FileInputStream(frompath);
            InputStreamReader isr = new InputStreamReader(fis);
            BufferedReader br = new BufferedReader(isr);
            
            fos = new FileOutputStream(topath);
            OutputStreamWriter osw = new OutputStreamWriter(fos);
            BufferedWriter bw = new BufferedWriter(osw);
            
            String tmp;
            while((tmp = br.readLine())!=null)
            {
                WebElement text = driver.findElement(By.xpath("//textarea[@id=\"TextBox2\"]"));
                System.out.println(text.getAttribute("id"));
                WebElement btn = driver.findElement(By.xpath("//input[@id=\"Button2\"]"));

                text.clear();
                text.sendKeys(tmp);
                btn.click();
                String ksql = driver.findElement(By.xpath("//*[@id=\"md5jie\"]")).getText();
                System.out.println(ksql);
                Thread.sleep(2000);
                
                bw.write(ksql);
                bw.newLine();
                bw.flush();
            }
            bw.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

 

posted on 2017-11-16 13:20  273286078  阅读(472)  评论(0编辑  收藏  举报