SeleniumConfig.java 2.65 KB
package com.canrd.webmagic.config;

import com.canrd.webmagic.driver.ChromeBuildDriver;
import com.google.common.collect.ImmutableList;
import com.google.gson.JsonObject;
import com.sun.java.swing.plaf.windows.resources.windows;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeDriverService;
import org.openqa.selenium.edge.EdgeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;

/**
 * @author: xms
 * @description: TODO
 * @date: 2024/4/26 14:37
 * @version: 1.0
 */
@Configuration
public class SeleniumConfig {
    private static String currentDriver = "";

    //    @Bean
    public WebDriver webDriver() throws InterruptedException, IOException {

        // 初始化ChromeOptions
        ChromeOptions chromeOptions = new ChromeBuildDriver().build("C:\\Program Files\\Google\\Chrome\\Application\\chromedriver.exe");

        EdgeOptions edgeOptions = new EdgeOptions();

        //配置Edge
        File edgeFile = new File("C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedgedriver.exe");

        edgeOptions.setPageLoadStrategy("none");
//        JsonObject jsonObject = edgeOptions.toJson();


        String os_name = System.getProperty("os.name");
        // 判断是否是windows系统
        if (os_name.toLowerCase().startsWith("win")) {

//            edgeOptions.setBinary("C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedgedriver.exe");
            currentDriver = "edge";
            //windows
            if (currentDriver.equals("") || currentDriver.equals("edge")) {
                System.out.printf("chrome启动");
                System.setProperty("webdriver.chrome.driver", "C:\\Program Files\\Google\\Chrome\\Application\\chromedriver.exe");
                currentDriver = "chrome";
                return new ChromeDriver(chromeOptions);
            } else if (currentDriver.equals("chrome")) {
                System.out.printf("edge启动");
                System.setProperty("webdriver.edge.driver", "C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedgedriver.exe");
                currentDriver = "edge";
                return new EdgeDriver(edgeOptions);
            }
        } else {
            // linux
            System.setProperty("webdriver.chrome.driver", "C:\\Program Files\\Google\\Chrome\\Application\\chromedriver.exe");
        }
        return null;
    }
}