selenium 抓取 自己 评论的腾讯新闻

package com.cyyun.test;

 

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.logging.Level;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
//import org.openqa.selenium.WebDriver.Navigation;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import com.gargoylesoftware.htmlunit.html.HtmlButton;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
 

 

public class CopyOfselenium  {
    
    

  public static void snapshot(TakesScreenshot drivername, String filename)
  {
      // this method will take screen shot ,require two parameters ,one is driver name, another is file name
      
      
        File scrFile = drivername.getScreenshotAs(OutputType.FILE);
        // Now you can do whatever you need to do with it, for example copy somewhere
        try {
            System.out.println("save snapshot path is:E:/"+filename);
            FileUtils.copyFile(scrFile, new File("E:\\"+filename));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("Can't save screenshot");
            e.printStackTrace();
        } 
        finally
        {
            System.out.println("screen shot finished");
        }
  }
  
  public static void snapshot2(WebDriver drivername, String filename)
  {
      // this method will take screen shot ,require two parameters ,one is driver name, another is file name
      
      
      //  File scrFile = drivername.getScreenshotAs(OutputType.FILE);
        // Now you can do whatever you need to do with it, for example copy somewhere
        try {
        	  WebDriver augmentedDriver = new Augmenter().augment(drivername);
              File screenshot = ((TakesScreenshot) augmentedDriver).getScreenshotAs(OutputType.FILE);
              File file = new File("E:\\"+filename);
              FileUtils.copyFile(screenshot, file);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("Can't save screenshot");
            e.printStackTrace();
        } 
        finally
        {
            System.out.println("screen shot finished");
        }
  }
  
  
   public static void login(WebDriver driver)throws Exception{
	   String login = "http://ui.ptlogin2.qq.com/cgi-bin/login?appid=46000101&style=13&lang=&low_login=1&hide_title_bar=1&hide_close_icon=1&self_regurl=http%3A//reg.t.qq.com/index.php&s_url=http%3A%2F%2Ft.qq.com&daid=6";
	   driver.get(login);
	   Thread.sleep(2000);
	   WebElement keyWord = driver.findElement(By.xpath("//input[@name='u']"));
	   keyWord.clear();
	   keyWord.sendKeys("QQuname");
	   keyWord = driver.findElement(By.xpath("//input[@name='p']"));
	   keyWord.clear();
	   keyWord.sendKeys("QQpwd");
	   
	   try{
		   keyWord = driver.findElement(By.xpath("//img[@id='verifyimg']"));
		   if(keyWord!=null){
			   String src =  keyWord.getAttribute("src");
			   System.out.println("验证码地址 ====="+src);
			   //验证码输入框
			   keyWord = driver.findElement(By.xpath("//input[@id='verifycode']"));   
			   keyWord.clear();
			   keyWord.sendKeys("");
		   }
	   }catch(Exception e){
		   
	   }
	   keyWord = driver.findElement(By.xpath("//input[@id='login_button']"));
	   keyWord.click();
	   Thread.sleep(5000);
   }
   
 

    public static void main (String [] args) throws Exception
    {
         
    	 String serverUrl = "http://192.168.1.107:4444/wd/hub";
        String URL="http://coral.qq.com/1008585338";//http://coral.qq.com/1008591939
        //System.setProperty("webdriver.chrome.driver", "d:\\ie\\chromedriver.exe"); 
        //WebDriver driver = new ChromeDriver();
        DesiredCapabilities capability = DesiredCapabilities.firefox();
        capability.setJavascriptEnabled(true);
        WebDriver driver = new RemoteWebDriver(new URL(serverUrl), capability);
       
        login(driver);
        
        driver.get(URL);
        //max size the browser
        driver.manage().window().maximize(); 
 /*
        Navigation navigation = driver.navigate();
         navigation.to(URL);*/
         Thread.sleep(2000);
         //snapshot((TakesScreenshot)driver,"open_baidu.png");
         snapshot2(driver,"open_baidu.png");
         //WebElement reg=driver.findElement(By.name("tj_reg"));
         //reg.click();
         //    WebElement keyWord = driver.findElement(By.id("kw1"));
         Thread.sleep(10000);
         //System.out.println(driver.getPageSource());
        // System.out.println(driver.getPageSource().contains("myCowComment"));
         
         //进入到iframe页面
         driver.switchTo().frame("commentIframe");
         System.out.println(driver.getPageSource());
         System.out.println(driver.getPageSource().contains("myCowComment"));
         
         //点击我的牛萍
         //find the element
         WebElement keyWord = driver.findElement(By.xpath("//li[@id='myCowComment']"));
        // keyWord.clear();
        //send key words 
         //keyWord.sendKeys("Selenium");
         keyWord.click();
         Thread.sleep(1000);
         
         keyWord = driver.findElement(By.xpath("//a[@id='mycon']"));
         keyWord.click();
         Thread.sleep(2000);
         
          snapshot2(driver,"input_keyWord.png");
         
 
         
        /*  WebElement submit = driver.findElement(By.id("su1"));
          
          System.out.println(submit.getLocation());
          submit.click();
          //System.out.println(driver.getWindowHandle());
         Thread.sleep(5000);
          
        // System.out.println(driver.getPageSource());
        
        String pageSource=driver.getPageSource();
       //  System.out.println(pageSource);
        //WebElement link =driver.findElement(By.xpath(SELENIUM_LINK));
        WebElement link =driver.findElement(By.xpath("//*[@id=\"1\"]/h3/a"));     //*[@id="1"]/h3/a
        link.click();
        Thread.sleep(5000); 
        driver.switchTo().window(driver.getWindowHandles().toArray(new String[0])[1]);
     
        //get page title
        System.out.println(driver.getTitle());
         Thread.sleep(5000);
    //     navigation.back();
          snapshot2(driver,"open_bake.png");
         System.out.println(driver.getTitle()+"\n"+driver.getCurrentUrl());*/
         
     
         
          driver.quit();
           
       
    }
    
}

 

 

你可能感兴趣的:(selenium)