Skip to content

Commit 4cb33bd

Browse files
committed
修复chrome114版本后无法下载chromedriver问题
1 parent 1ae7ba4 commit 4cb33bd

2 files changed

Lines changed: 49 additions & 29 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
</parent>
99
<groupId>com.iceolive</groupId>
1010
<artifactId>selenium-script</artifactId>
11-
<version>0.4.4</version>
11+
<version>0.4.5</version>
1212
<description>基于selenium-java的自定义脚本语言工具类</description>
1313
<licenses>
1414
<license>

src/main/java/com/iceolive/selenium/ChromeUtil.java

Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -41,27 +41,30 @@ public static void killChromeDriver() {
4141

4242
public static String getVersion() {
4343
String version = RegUtil.getValue("\"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Google Chrome\"", "DisplayVersion");
44-
if(version!=null){
44+
if (version != null) {
4545
return version;
4646
}
47-
version = RegUtil.getValue("\"HKEY_LOCAL_MACHINE\\SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Google Chrome\"","DisplayVersion");
47+
version = RegUtil.getValue("\"HKEY_LOCAL_MACHINE\\SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Google Chrome\"", "DisplayVersion");
4848
return version;
4949
}
5050

51-
public static void unzip(File file, String destDirPath) throws IOException {
51+
public static void unzip(File file, String destDirPath, String ignorePath) throws IOException {
5252

5353
log.info("开始解压文件...");
5454
ZipFile zipFile = new ZipFile(file);
5555
Enumeration<?> entries = zipFile.entries();
5656

5757
while (entries.hasMoreElements()) {
5858
ZipEntry entry = (ZipEntry) entries.nextElement();
59-
59+
String name = entry.getName();
60+
if (name.startsWith(ignorePath)) {
61+
name = name.substring(ignorePath.length());
62+
}
6063

6164
// 如果是文件夹,就创建个文件夹
6265

6366
if (entry.isDirectory()) {
64-
String dirPath = destDirPath + "/" + entry.getName();
67+
String dirPath = destDirPath + "/" + name;
6568

6669
File dir = new File(dirPath);
6770

@@ -70,7 +73,7 @@ public static void unzip(File file, String destDirPath) throws IOException {
7073
} else {
7174
// 如果是文件,就先创建一个文件,然后用io流把内容copy过去
7275

73-
File targetFile = new File(destDirPath + "/" + entry.getName());
76+
File targetFile = new File(destDirPath + "/" + name);
7477

7578
// 保证这个文件的父文件夹必须要存在
7679

@@ -114,27 +117,40 @@ private static void downloadAndUnzip() throws IOException {
114117
log.error("请先安装chrome浏览器");
115118
}
116119
version = version.split("\\.")[0];
120+
117121
Request request;
118122
Response response;
119-
try {
120-
String url = domain + "/LATEST_RELEASE_" + version;
121-
request = new Request.Builder()
122-
.url(url)
123-
.get()
124-
.build();
125-
response = client.newCall(request).execute();
126-
} catch (Exception e) {
127-
domain = chromeDriverDomain;
128-
String url = domain + "/LATEST_RELEASE_" + version;
123+
String downloadUrl;
124+
if (Integer.parseInt(version) <= 114) {
125+
try {
126+
String url = domain + "/LATEST_RELEASE_" + version;
127+
request = new Request.Builder()
128+
.url(url)
129+
.get()
130+
.build();
131+
response = client.newCall(request).execute();
132+
} catch (Exception e) {
133+
domain = chromeDriverDomain;
134+
String url = domain + "/LATEST_RELEASE_" + version;
135+
request = new Request.Builder()
136+
.url(url)
137+
.get()
138+
.build();
139+
response = client.newCall(request).execute();
140+
}
141+
String fullVersion = response.body().string();
142+
downloadUrl = domain + "/" + fullVersion + "/chromedriver_win32.zip";
143+
} else {
144+
String url = "https://googlechromelabs.github.io/chrome-for-testing/LATEST_RELEASE_" + version;
129145
request = new Request.Builder()
130146
.url(url)
131147
.get()
132148
.build();
133149
response = client.newCall(request).execute();
134-
150+
String fullVersion = response.body().string();
151+
downloadUrl = "https://storage.googleapis.com/chrome-for-testing-public" + "/" + fullVersion + "/win32/chromedriver-win32.zip";
135152
}
136-
String fullVersion = response.body().string();
137-
String downloadUrl = domain + "/" + fullVersion + "/chromedriver_win32.zip";
153+
138154
log.info("开始下载chromedriver...");
139155
request = new Request.Builder().url(downloadUrl).build();
140156
response = client.newCall(request).execute();
@@ -151,8 +167,12 @@ private static void downloadAndUnzip() throws IOException {
151167
fos.flush();
152168
is.close();
153169
fos.close();
170+
if (Integer.parseInt(version) <= 114) {
171+
unzip(new File(downloadPath), System.getProperty("user.dir"),"");
172+
}else{
173+
unzip(new File(downloadPath), System.getProperty("user.dir"),"chromedriver-win32/");
174+
}
154175

155-
unzip(new File(downloadPath), System.getProperty("user.dir"));
156176

157177
}
158178

@@ -185,7 +205,7 @@ public static void main(String[] args) throws IOException {
185205
ChromeServer chromeServer = new ChromeServer(port);
186206
chromeServer.start();
187207
} else {
188-
log.info("当前版本:"+VersionUtil.getVersion());
208+
log.info("当前版本:" + VersionUtil.getVersion());
189209
String s = FileUtil.readFromFile(script, "utf-8");
190210
runScript(s, driver, proxy);
191211
}
@@ -197,30 +217,30 @@ public static void runScript(String script, String driver, String proxy) throws
197217

198218
BrowserMobProxy browserMobProxy = new BrowserMobProxyServer();
199219
if (StringUtils.isNotEmpty(proxy)) {
200-
if(proxy.contains(":")){
220+
if (proxy.contains(":")) {
201221
InetSocketAddress address = new InetSocketAddress(proxy.split(":")[0], Integer.parseInt(proxy.split(":")[1]));
202222
browserMobProxy.setChainedProxy(address);
203223
enableMob = true;
204224
}
205225
}
206-
if(enableMob) {
226+
if (enableMob) {
207227
browserMobProxy.start(0);
208228
}
209229
ChromeWebDriver webDriver;
210230
try {
211231
if (enableMob) {
212-
webDriver = new ChromeWebDriver(driver,headless, browserMobProxy);
232+
webDriver = new ChromeWebDriver(driver, headless, browserMobProxy);
213233
} else {
214-
webDriver = new ChromeWebDriver(driver,headless);
234+
webDriver = new ChromeWebDriver(driver, headless);
215235
}
216236
} catch (Exception e) {
217237
if (e.getMessage().contains("only supports Chrome version")) {
218238
log.error("chromedriver版本不匹配!");
219239
downloadAndUnzip();
220240
if (enableMob) {
221-
webDriver = new ChromeWebDriver(driver,headless, browserMobProxy);
241+
webDriver = new ChromeWebDriver(driver, headless, browserMobProxy);
222242
} else {
223-
webDriver = new ChromeWebDriver(driver,headless);
243+
webDriver = new ChromeWebDriver(driver, headless);
224244
}
225245
} else {
226246
throw e;
@@ -243,7 +263,7 @@ public static void runScript(String script, String driver, String proxy) throws
243263
} catch (Exception e) {
244264
log.error("执行出错:" + e.toString());
245265
}
246-
if(headless){
266+
if (headless) {
247267
//无浏览器窗口模式执行完毕需要关闭对应的webDriver
248268
if (enableMob) {
249269
browserMobProxy.stop();

0 commit comments

Comments
 (0)