package org.jeecg.modules.spyderUtils.spider;

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.logging.LogType;
import org.openqa.selenium.logging.LoggingPreferences;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.logging.Level;

@Component
public class CreateChromeDriver {

    public RemoteWebDriver createChromeDriver() {
        //加载ChromeDriver 是使用Chrome的必要条件
        System.getProperties().setProperty("webdriver.chrome.driver","C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe");
        //添加chrome的配置信息
        ChromeOptions chromeOptions = new ChromeOptions();
        //设置为无头模式
       // chromeOptions.addArguments("--headless");
        //设置打开的窗口大小非必要属性
        chromeOptions.addArguments("--window-size=1920,1080");
        chromeOptions.addArguments("sec-fetch-site=same-site");
        chromeOptions.addArguments("--disable-blink-features");
        chromeOptions.addArguments("--disable-blink-features=AutomationControlled");
        chromeOptions.addArguments("no-sandbox");//禁用沙盒
        chromeOptions.addArguments("--remote-allow-origins=*");
        chromeOptions.setExperimentalOption("useAutomationExtension", false);
        chromeOptions.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
        //使用配置信息创建driver对象
        RemoteWebDriver driver = new ChromeDriver(chromeOptions);
        return driver;
    }
    public RemoteWebDriver createChromeDriverLog() {

        //加载ChromeDriver 是使用Chrome的必要条件
        System.getProperties().setProperty("webdriver.chrome.driver","C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe");
        //添加chrome的配置信息
        ChromeOptions chromeOptions = new ChromeOptions();
        //设置为无头模式
       // chromeOptions.addArguments("--headless");
        //设置打开的窗口大小非必要属性
        chromeOptions.addArguments("--window-size=1920,1080");
        chromeOptions.addArguments("sec-fetch-site=same-site");
        chromeOptions.addArguments("--disable-blink-features");
        chromeOptions.addArguments("--disable-blink-features=AutomationControlled");
        chromeOptions.addArguments("no-sandbox");//禁用沙盒
        chromeOptions.addArguments("--remote-allow-origins=*");
        chromeOptions.setExperimentalOption("useAutomationExtension", false);
        chromeOptions.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
        //使用配置信息创建driver对象
        LoggingPreferences logPrefs = new LoggingPreferences();
        logPrefs.enable(LogType.BROWSER, Level.ALL);
        chromeOptions.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
        RemoteWebDriver driver = new ChromeDriver(chromeOptions);
        return driver;
    }
}
