selenium常用参数配置
经常使用selenium开发,这里记录一些常用启动参数,以备后用。
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
public static void main(String[] args) {
// 设置驱动以及浏览器路径
System.setProperty("webdriver.chrome.driver", "D:\\workspace\\cesi_cn_spider\\chromedriver-win64\\chromedriver.exe");
System.setProperty("webdriver.chrome.bin", "D:\\workspace\\cesi_cn_spider\\chrome-win64\\chrome.exe");
System.setProperty("webdriver.chrome.whitelistedIps", "");
ChromeOptions opt = new ChromeOptions();
// 设置浏览器路径
opt.setBinary("D:\\workspace\\cesi_cn_spider\\chrome-win64\\chrome.exe");
// 启动后窗口最大化
opt.addArguments("start-maximized");
opt.addArguments("disable-infobars");
// 启动时自动打开调试窗口
// opt.addArguments("--auto-open-devtools-for-tabs");
// 设置窗口大小
opt.addArguments("--window-size=2560x1440");
opt.addArguments("--disable-cache");
// 关闭网页缓存
opt.addArguments("--disable-application-cache");
opt.addArguments("--disk-cache-size=0");
opt.addArguments("--disable-gpu"); // applicable to windows os only
opt.addArguments("--disable-dev-shm-usage"); // overcome limited resource problems
opt.addArguments("--dns-prefetch-disable");
opt.addArguments("--disable-notifications");
opt.addArguments("disable-infobars");
// 禁止远程调试
opt.setExperimentalOption("detach", true);
// 关闭跨域
opt.addArguments("--disable-web-security");
opt.addArguments("--disable-site-isolation-trials");
// 关闭扩展
// opt.addArguments("--disable-extensions");
// 加载自定义扩展
opt.addArguments("--load-extension=%s".formatted("D:\\workspace\\cesi_cn_spider\\chromeext"));
ChromeDriver browser = null;
// 自定义下载路径,及关闭下载提示
Map<String, Object> prefs = new HashMap<>();
prefs.put("download.default_directory", "path to download");
prefs.put("browser.download.dir", "path to download");
prefs.put("download.prompt_for_download", false); // Optional: disable the "Save As" dialog
opt.setExperimentalOption("prefs", prefs);
browser = new ChromeDriver( opt);
browser.manage().window().setSize(new Dimension(2560, 1440));
browser.executeCdpCommand("Network.setCacheDisabled",new HashMap<>());
browser.get("http://www.baidu.com");
}
java项目主要依赖:
1
2
3
4
5
6
7
8
9
10
11
dependencies {
// https://mvnrepository.com/artifact/com.alibaba/fastjson
implementation group: 'com.alibaba', name: 'fastjson', version: '2.0.49'
// implementation 'org.seleniumhq.selenium:selenium-edge-driver:4.12.1'
//implementation 'org.seleniumhq.selenium:selenium-firefox-driver:4.12.1'
implementation 'org.seleniumhq.selenium:selenium-chrome-driver:4.18.0'
implementation 'org.seleniumhq.selenium:selenium-support:4.18.0'
}
This post is licensed under
CC BY 4.0
by the author.