SeleniumConfig.java
2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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;
}
}