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();
           
       
    }
    
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值