package com.zzsn.utility.util;

import com.zzsn.utility.index.Constants;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.util.ArrayList;
import java.util.List;

public class ChromeUtil {
	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);
//		options1.addArguments("user-data-dir=C:\\Users\\WIN10\\AppData\\Local\\Google\\Chrome\\User Data\\Default");
//		options1.addArguments("--headless");
	      /*  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);*/
//		options1.addArguments("blink-settings=imagesEnabled=false");

//		driver = new ChromeDriver(options1);
	}
	public static String getChromeDoc(String url,String start){
		try {
			driver.get("https://www.google.com");
		} catch (Exception e1) {
			//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(5000);
			searchBox2.sendKeys(Keys.ENTER);
			Thread.sleep(2000);
			String local=driver.getCurrentUrl();
			String realUrl=local+"&&tbs=lr%3Alang_1en%2Ccdr%3A1%2Ccd_min%3A12%2F14%2F2020%2Ccd_max%3A12%2F29%2F2020&hl=en&lr=lang_en&tbm=&num=20&start="+start;
			driver.get(realUrl);

			Thread.sleep(2000);
		/*WebElement towe=driver.findElement(By.linkText("2"));
		if(towe!=null){
			towe.click();
			Thread.sleep(2000);
		}else{
			return null;
		}*/
			String doc=driver.getPageSource();
			//	driver.close();
			return doc;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		/**
		 * dr.quit()和dr.close()都可以�?出浏览器
		 * 如果打开了多个页面是关不干净的，它只关闭当前的一个页面？
		 */
		return null;
	}

	public static String getChromeDocnews(String url, String start) {

		//新建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(1000);
			searchBox2.sendKeys(Keys.ENTER);
			Thread.sleep(1000);
			String local = driver.getCurrentUrl();
			String realUrl=local+"&tbs=lr%3Alang_1en%2Ccdr%3A1%2Ccd_min%3A12%2F14%2F2020%2Ccd_max%3A12%2F29%2F2020&hl=en&lr=lang_en&tbm=&num=20&start="+start;
//			String realUrl = local + "&tbs=lr:lang_1en&hl=en&lr=lang_en&tbm=nws&num=100&start="+start;
			driver.get(realUrl);

//			Thread.sleep(1000);
//		WebElement towe=driver.findElement(By.linkText("2"));
//		if(towe!=null){
//			towe.click();
//			Thread.sleep(2000);
//		}else{
//			return null;
//		}
			String doc = driver.getPageSource();
			//	driver.close();
			driver.quit();
			return doc;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			driver.quit();
		}
		return null;
	}


	public static String getChromeDoc(String url){
		WebDriver driver= new ChromeDriver(options1);
	             //新建????WebDriver 的对象，但是new 的是FirefoxDriver的驱??
//	        try {
//				driver.get("https://www.google.com");
//				//打开指定的网??
//			} catch (Exception e1) {
//				  driver =new ChromeDriver(options1);
//				  driver.get("https://www.google.com");
//			}
	        try {
	        	driver.get(url);
				Thread.sleep(2000);
//				WebElement towe=driver.findElement(By.linkText("2"));
//				if(towe!=null){
//					towe.click();
//					Thread.sleep(2000);
//				}else{
//					return null;
//				}
				String doc=driver.getPageSource();
				Thread.sleep(3000);
//				driver.close();
				driver.quit();
				return doc;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				driver.quit();
			}
	        
	        /**
	         * dr.quit()和dr.close()都可以??出浏览器,????的说????两??的区别：第一个close??
	         * 如果打开了多个页面是关不干净的，它只关闭当前的一个页面??第二个quit??
	         * 是??出了????Webdriver????的窗口，????非常干净，所以推荐使用quit????????case????的方法??
	         */
		return null;
	}

	public static String getDoc(String url){
		driver = new ChromeDriver(options1);

		//新建�?��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 {

			Thread.sleep(4000);
			String doc=driver.getPageSource();
			driver.quit();
			return doc;
		} catch (Exception e) {
			driver.quit();
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		/**
		 * dr.quit()和dr.close()都可以�?出浏览器,�?��的说�?��两�?的区别：第一个close�?
		 * 如果打开了多个页面是关不干净的，它只关闭当前的一个页面�?第二个quit�?
		 * 是�?出了�?��Webdriver�?��的窗口，�?��非常干净，所以推荐使用quit�?���?��case�?��的方法�?
		 */
		return null;
	}

	public static String getChromeDocKey(String url,String start){

		//新建�?��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(5000);
			searchBox2.sendKeys(Keys.ENTER);
			Thread.sleep(2000);
			String local=driver.getCurrentUrl();
			String realUrl=local+"&tbs=lr:lang_1en,sbd:1&hl=en&lr=lang_en&tbm=nws&num=20&start="+start;
			driver.get(realUrl);

			Thread.sleep(2000);
		/*WebElement towe=driver.findElement(By.linkText("2"));
		if(towe!=null){
			towe.click();
			Thread.sleep(2000);
		}else{
			return null;
		}*/
			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) {
		
		ChromeUtil.getChromeDoc("https://www.baidu.com/s?rtt=4&bsst=1&cl=2&tn=news&rsv_dl=ns_pc&lqst=1&x_bd_lqst=1&word=%E5%A4%A9%E6%B4%A5+%E6%B7%B7%E6%94%B9&pn=60");
		
	}
}
