下载selenium服务端
下载浏览器驱动
服务端启动
服务启动命令
shell
java -jar selenium-server-standalone-3.141.59.jar
1
客户端代码
java
@Configuration
public class WebDriverConfig {
@Bean(destroyMethod = "quit")
public WebDriver webDriver() throws IOException {
ChromeOptions options = new ChromeOptions();
//--user-agent="" 设置请求头的User-Agent
//--window-size=1366,768 设置浏览器分辨率(窗口大小)
//--headless 无界面运行(无窗口)
//--start-maximized 最大化运行(全屏窗口)
//--incognito 隐身模式(无痕模式)
//--disable-javascript 禁用javascript
//--disable-infpygmentsobars 禁用浏览器正在被自动化程序控制的提示
// 无痕模式 最大化
options.addArguments("--incognito", "--start-maximized", "--whitelisted-ips=192.168.2.1");
// 使用远程驱动
return new RemoteWebDriver(new URL("http://192.168.2.86:4444/wd/hub"), options);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
selenium grid
通过-role hub/-role node来指定是hub还是node,hub至少应该有一个node节点,node节点通过http://ip:port/grid/register/注册至hub。 客户端通过hub连接,hub将请求转发至node节点执行。
参数配置方式
shell
## 启动hub
java -jar selenium-server-standalone-3.141.59.jar -role hub -maxSession 10 -port 4444
## 启动node
java -jar -"Dwebdriver.chrome.driver=C:\\tools\\driver\\chromedriver.exe" selenium-server-standalone-3.141.59.jar -role node -hub http://localhost:4444/grid/register -port 4445
1
2
3
4
2
3
4
配置文件方式
shell
## 启动hub
java -jar selenium-server-standalone-3.141.59.jar -role hub -hubConfig hub.json
## 启动node
java -jar selenium-server-standalone-3.141.59.jar -role node -hub http://localhost:4444/grid/register -nodeConfig node.json
1
2
3
4
2
3
4
- hub.json
json
{
"port": 4444,
"newSessionWaitTimeout": -1,
"servlets": [],
"capabilityMatcher": "org.openqa.grid.internal.utils.DefaultCapabilityMatcher",
"throwOnCapabilityNotPresent": true,
"nodePolling": 5000,
"cleanUpCycle": 5000,
"timeout": 60,
"browserTimeout": 60,
"maxSession": 10,
"jettyMaxThreads": -1
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
- node.json
json
{
"capabilities": [
{
"browserName": "firefox",
"marionette": true,
"maxInstances": 1,
"seleniumProtocol": "WebDriver"
},
{
"browserName": "chrome",
"maxInstances": 3,
"platform": "WINDOWS",
"webdriver.chrome.driver": "D:\\javaproject\\oATFWeb\\External\\chromedriver2.4.1.exe",
"seleniumProtocol": "WebDriver"
},
{
"browserName": "internet explorer",
"platform": "WINDOWS",
"maxInstances": 1,
"seleniumProtocol": "WebDriver"
},
{
"browserName": "safari",
"technologyPreview": false,
"platform": "MAC",
"maxInstances": 1,
"seleniumProtocol": "WebDriver"
}
],
"proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
"maxSession": 5,
"port": 5555,
"register": true,
"registerCycle": 5000,
"hub": "http://10.12.1.140:4444",
"nodeStatusCheckTimeout": 5000,
"nodePolling": 5000,
"role": "node",
"unregisterIfStillDownAfter": 60000,
"downPollingLimit": 2,
"debug": false,
"servlets": [],
"withoutServlets": [],
"custom": {},
"browserTimeout": 60,
"timeout": 60
}
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
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
结果
访问http://hub.ip:port/grid/console
可以看到node的详细配置