package com.zzsn.search;

import java.util.HashMap;
import java.util.Map;

import com.zzsn.util.Constants;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class ChromeUtilzhi {
	static ChromeOptions    options1=new ChromeOptions() ;
	static WebDriver driver;
	static{
		  System.setProperty("webdriver.chrome.driver", Constants.CHROMEDRIVE);//chromedriver服务地址
	        System.setProperty("webdriver.chrome.bin", Constants.CHROMEBIN); 
	       
	        options1.setBinary(Constants.CHROMEBIN); 
	        Map<String, Object> contentSettings = new HashMap<String, Object>();
	        contentSettings.put("images", 2);
	         
	        Map<String, Object> preferences = new HashMap<String, Object>();
	        preferences.put("profile.default_content_settings", contentSettings);

	   

	        driver =new ChromeDriver(options1);
	}

	public static String getChromeDoc(String url){
		
	             //新建�?��WebDriver 的对象，但是new 的是FirefoxDriver的驱�?
	        try {
				driver.get("https://www.google.com");
				//打开指定的网�?
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				//e1.printStackTrace();
				  driver =new ChromeDriver(options1);
				  driver.get("https://www.google.com");
			}
	        try {
	        	WebElement searchBox2 = driver.findElement(By.name("q"));
	        	searchBox2.sendKeys(url);
	        	Thread.sleep(2000);
	        	WebElement searchBox3 = driver.findElement(By.name("btnK"));
	        	searchBox3.click();
	        	Thread.sleep(2000);
	        	String local=driver.getCurrentUrl();
	        	String realUrl=local+"&hl=en&lr=lang_en&tbm=nws&num=20&&tbs=qdr:d";
	        	driver.get(realUrl);
				Thread.sleep(2000);
				String doc=driver.getPageSource();
			//	driver.close();
				return doc;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        
	        /**
	         * dr.quit()和dr.close()都可以�?出浏览器,�?��的说�?��两�?的区别：第一个close�?
	         * 如果打开了多个页面是关不干净的，它只关闭当前的一个页面�?第二个quit�?
	         * 是�?出了�?��Webdriver�?��的窗口，�?��非常干净，所以推荐使用quit�?���?��case�?��的方法�?
	         */
		return null;
	}
	
	public static String getSougouweixin(String url){
		
        //新建�?��WebDriver 的对象，但是new 的是FirefoxDriver的驱�?
   try {
	   driver.get(url);
		//打开指定的网�?
	} catch (Exception e1) {
		// TODO Auto-generated catch block
		//e1.printStackTrace();
		  driver =new ChromeDriver(options1);
		  driver.get(url);
	}
	   try {
	   	WebElement searchBox2 =null;
		try {
			searchBox2 = driver.findElement(By.id("showWantMore"));
			Thread.sleep(2000);
			searchBox2.click();
			Thread.sleep(2000);
			WebElement searchBox3 = driver.findElement(By.id("username"));
			WebElement searchBox4 = driver.findElement(By.id("password"));
			searchBox3.sendKeys("328345796@qq.com");
			searchBox4.sendKeys("li8329777");
			WebElement searchBox5= driver.findElement(By.id("submittext"));
			Thread.sleep(2000);
			searchBox5.click();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("不需要登陆");
		}
	   //	searchBox2.sendKeys(key);
   	
   	//String local=driver.getCurrentUrl();
   	//String realUrl=local+"&tsn=1";
   	//driver.get(realUrl);
		Thread.sleep(2000);
		String doc=driver.getPageSource();
	//	driver.close();
		return doc;
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
   
   /**
    * dr.quit()和dr.close()都可以�?出浏览器,�?��的说�?��两�?的区别：第一个close�?
    * 如果打开了多个页面是关不干净的，它只关闭当前的一个页面�?第二个quit�?
    * 是�?出了�?��Webdriver�?��的窗口，�?��非常干净，所以推荐使用quit�?���?��case�?��的方法�?
    */
return null;
}
	public static void main(String[] args) {
		
		ChromeUtilzhi.getSougouweixin("http://expert.cnki.net/Expert/Detail/05975529.htm");
		//ChromeUtilzhi.getSougouweixin("http://expert.cnki.net/Expert/Detail/05966394.htm");
	}
}
