Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2180147
  • 博文数量: 104
  • 博客积分: 206
  • 博客等级: 入伍新兵
  • 技术积分: 1829
  • 用 户 组: 普通用户
  • 注册时间: 2012-09-12 10:24
个人简介

效字当先,以质为本。测试开发入行十余年,辉煌过,迷茫过,持续探寻人生的激情和前进的步伐!好好生活,认真工作!

文章分类
文章存档

2024年(1)

2019年(2)

2018年(4)

2017年(7)

2016年(3)

2015年(14)

2014年(33)

2013年(31)

2012年(9)

分类: Java

2015-09-18 15:15:41


点击(此处)折叠或打开

  1. package zhaohui;

  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8. import java.sql.DriverPropertyInfo;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. import java.util.logging.Level;

  12. import org.apache.commons.io.FileUtils;
  13. import org.openqa.selenium.By;
  14. import org.openqa.selenium.OutputType;
  15. import org.openqa.selenium.TakesScreenshot;
  16. import org.openqa.selenium.WebDriver;
  17. import org.openqa.selenium.WebElement;
  18. import org.openqa.selenium.logging.LogType;
  19. import org.openqa.selenium.logging.LoggingPreferences;
  20. import org.openqa.selenium.phantomjs.PhantomJSDriver;
  21. import org.openqa.selenium.phantomjs.PhantomJSDriverService;
  22. import org.openqa.selenium.remote.CapabilityType;
  23. import org.openqa.selenium.remote.DesiredCapabilities;



  24. public class Yaogeili {
  25.     private String phantomjsPath = null;
  26.     private WebDriver driver = null;
  27.     private DesiredCapabilities caps = null;
  28.     
  29.     public Yaogeili(String path, String ua) {
  30.         this.phantomjsPath = path;
  31.         caps = DesiredCapabilities.phantomjs();
  32.         caps.setJavascriptEnabled(true);
  33.         caps.setCapability("takesScreenshot", true);
  34.         caps.setCapability(
  35.                 PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
  36.                 this.phantomjsPath);
  37.         caps.setCapability("phantomjs.page.settings.userAgent", ua);
  38.         caps.setCapability("output-encoding", "UTF-8");
  39.         caps.setCapability("script-encoding", "UTF-8");
  40.         //关闭phantomjs的log,目前只能减少log信息,无法彻底关闭
  41.         LoggingPreferences logPrefs = new LoggingPreferences();
  42.         logPrefs.enable(LogType.BROWSER, Level.OFF);
  43.         logPrefs.enable(LogType.DRIVER, Level.OFF);
  44.         logPrefs.enable(LogType.CLIENT, Level.OFF);
  45.         logPrefs.enable(LogType.PERFORMANCE, Level.OFF);
  46.         logPrefs.enable(LogType.PROFILER, Level.OFF);
  47.         logPrefs.enable(LogType.SERVER, Level.OFF);    
  48.         caps.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
  49.         this.driver=new PhantomJSDriver(caps);
  50.     }
  51.     
  52.     public void gethtml(String query){
  53.         String url = " style="color:#0000CC;">+java.net.URLEncoder.encode(query)+"&type=m-map_bus2";
  54. //        System.setProperty("webdriver.chrome.driver", "files/chromedriver.exe");
  55. //        WebDriver driver = new ChromeDriver();
  56.         this.driver.get(url);
  57.         driver.manage().window().maximize();
  58.         try {
  59.             Thread.sleep(1000);
  60.         } catch (InterruptedException e1) {
  61.             // TODO Auto-generated catch block
  62.             e1.printStackTrace();
  63.         }
  64.         try{
  65.             WebElement webelement =this.driver.findElement(By.xpath("//*[@id='J-iframe-html']/iframe"));
  66.             this.driver.switchTo().frame(webelement);
  67.         }catch(Exception e){
  68.             System.out.println(e);
  69.         }            
  70.         try{
  71.             this.driver.findElement(By.xpath("//*[@id='mohe-m-super-map-medicine-broad']"));
  72.             System.out.println(query+"ok");
  73.         }
  74.         catch (Exception e){
  75.             this.takeScreen("test-output/screen", query);
  76.             System.out.println(query);    
  77.         }
  78. //        this.driver.quit();    
  79.     }
  80.     public String takeScreen(String screenParentPath,String screenName)
  81.     {
  82.         this.driver.getCurrentUrl();
  83.         File file=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
  84.         String screenAdress=null;
  85.         try {
  86.             FileUtils.copyFile(file, new File(screenParentPath+System.getProperty("file.separator")+screenName+".jpg"));
  87.         } catch (FileNotFoundException e) {
  88.             e.printStackTrace();
  89.         } catch (IOException e) {
  90.             e.printStackTrace();
  91.         }
  92.         finally    
  93.         {
  94.             screenAdress=screenName+".png";            
  95.         }
  96.         return screenAdress;
  97.     }        
  98.     
  99.     @SuppressWarnings({ "rawtypes", "unchecked" })
  100.     public List<String> getquery() throws IOException{
  101.         File file = new File("files\\files.txt");
  102.         String encoding="GBK";
  103.         List list=new ArrayList();
  104.         if (file.isFile()&& file.exists()){
  105.             InputStreamReader read = new InputStreamReader(new FileInputStream(file),encoding);
  106.             BufferedReader bufferedReader = new BufferedReader(read);
  107.             String lineTxt = null;
  108.             while((lineTxt = bufferedReader.readLine()) != null){
  109.                 list.add(lineTxt);
  110.             }
  111.             read.close();
  112.             
  113.         }else{
  114.             System.out.println("query文件不存在");
  115.         }
  116.         return list;

  117.     }
  118.     

  119.     public static void main(String[] args) throws IOException {
  120.         String ua = "Mozilla/5.0 (Windows; U; Windows NT 5.2) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.27 Safari/525.13 ";
  121.         Yaogeili yl = new Yaogeili("src/zhaohui/phantomjs.exe", ua);
  122. //        yl.gethtml("21金维他");
  123.         List<String> list = yl.getquery();
  124.         for(int i=0;i<list.size();i++){
  125.             yl.gethtml(list.get(i));
  126.         }
  127.         yl.driver.quit();


  128.     }

  129. }

阅读(2558) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~