张延森 4 years ago
parent
commit
aa153e79a9

+ 4
- 0
README.md View File

@@ -8,6 +8,10 @@
8 8
 ```bash
9 9
 # yum install google-chrome-stable 方式二选一
10 10
 curl https://intoli.com/install-google-chrome.sh | sh
11
+
12
+# 查看 chrome 版本
13
+# 默认安装位置 /opt/google/chrome
14
+google-chrome-stable --version
11 15
 ```
12 16
 
13 17
 #### 下载 chrome 驱动

+ 4
- 2
pom.xml View File

@@ -10,7 +10,7 @@
10 10
 	</parent>
11 11
 	<groupId>com.shigongli</groupId>
12 12
 	<artifactId>shigongli</artifactId>
13
-	<version>1.0.13</version>
13
+	<version>1.0.14</version>
14 14
 	<name>shigongli</name>
15 15
 	<description>ShiGongli Service</description>
16 16
 
@@ -137,12 +137,14 @@
137 137
 		<!--swagger end-->
138 138
 
139 139
 		<!-- selenium-java -->
140
+		<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
140 141
 		<dependency>
141 142
 			<groupId>org.seleniumhq.selenium</groupId>
142 143
 			<artifactId>selenium-java</artifactId>
143
-			<version>3.4.0</version>
144
+			<version>3.141.59</version>
144 145
 		</dependency>
145 146
 
147
+
146 148
 		<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
147 149
 		<dependency>
148 150
 			<groupId>com.google.guava</groupId>

+ 45
- 16
src/main/java/com/shigongli/common/ScreenShotUtil.java View File

@@ -1,17 +1,22 @@
1 1
 package com.shigongli.common;
2 2
 
3 3
 import com.shigongli.config.ScreenShotConfig;
4
+import lombok.extern.slf4j.Slf4j;
5
+import org.checkerframework.checker.nullness.qual.Nullable;
4 6
 import org.openqa.selenium.*;
5 7
 import org.openqa.selenium.chrome.ChromeDriver;
6 8
 import org.openqa.selenium.chrome.ChromeOptions;
9
+//import org.openqa.selenium.devtools.DevTools;
10
+//import org.openqa.selenium.devtools.network.Network;
7 11
 import org.openqa.selenium.support.ui.ExpectedCondition;
8 12
 import org.openqa.selenium.support.ui.WebDriverWait;
9 13
 import org.springframework.beans.factory.annotation.Autowired;
10 14
 import org.springframework.stereotype.Component;
11 15
 
12 16
 import java.io.File;
13
-import java.util.concurrent.TimeUnit;
17
+//import java.util.Optional;
14 18
 
19
+@Slf4j
15 20
 @Component
16 21
 public class ScreenShotUtil {
17 22
     @Autowired
@@ -20,11 +25,11 @@ public class ScreenShotUtil {
20 25
     @Autowired
21 26
     ScreenShotConfig config;
22 27
 
23
-    public String shot(String url, String cssSelector) throws Exception {
28
+    public String shot(String url) throws Exception {
24 29
         //驱动地址
25 30
         System.setProperty("webdriver.chrome.driver", config.getDriver());
26 31
 
27
-        WebDriver webDriver = null;
32
+        WebDriver driver = null;
28 33
         ChromeOptions options=new ChromeOptions();
29 34
         //设置 chrome 的无头模式
30 35
         options.addArguments("--headless");
@@ -32,35 +37,59 @@ public class ScreenShotUtil {
32 37
         options.addArguments("--no-sandbox");
33 38
         options.addArguments("--disable-dev-shm-usage");
34 39
         options.addArguments("--start-maximized");
40
+        options.addArguments("--disable-application-cache");
35 41
         options.addArguments("--lang=zh");
36 42
         options.addArguments(String.format("--window-size=%s,%s", config.getWidth(), config.getHeight()));
37 43
         //启动一个 chrome 实例
38
-        webDriver = new ChromeDriver(options);
44
+        driver = new ChromeDriver(options);
39 45
 
40 46
         //访问网址
41
-        webDriver.get(url);
47
+        log.info(String.format("访问网址: %s", url));
48
+//        webDriver.get(url);
49
+
50
+//        DevTools devTools = driver.getDevTools();
51
+//        devTools.createSession();
52
+//        devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.of(100000000)));
53
+//        devTools.send(Network.setCacheDisabled(true));
54
+//        devTools.addListener(Network.responseReceived(), responseReceived -> Assert.assertEquals(false, responseReceived.getResponse().getFromDiskCache()));
55
+
56
+        driver.get(url);
57
+
58
+//        devTools.send(Network.clearBrowserCache());
42 59
 
43 60
         // 设置超时等待
44
-        WebDriverWait webDriverWait = new WebDriverWait(webDriver, config.getWait() / 1000);
45
-        String finalCssSelector = null == cssSelector ? "body" : cssSelector;
46
-        webDriverWait.until(new ExpectedCondition<WebElement>() {
47
-            @Override
48
-            public WebElement apply(WebDriver driver) {
49
-                // 通过定位页面元素来确定页面确实正常加载了
50
-                return driver.findElement(By.cssSelector(finalCssSelector));
51
-            }
52
-        });
61
+        try {
62
+            WebDriverWait webDriverWait = new WebDriverWait(driver, config.getWait() / 1000, 200);
63
+            webDriverWait.until(new ExpectedCondition<WebElement>() {
64
+                public @Nullable WebElement apply(@Nullable WebDriver driver) {
65
+                    WebElement el = null;
66
+                    try {
67
+                        WebDriver wf = driver.switchTo().frame(0);
68
+                        log.info("Got Web Iframe {}", wf);
69
+                        el = wf.findElement(By.cssSelector("body"));
70
+                        log.info("Got Web Element {}", el);
71
+                    } catch (Exception e) {
72
+                        e.printStackTrace();
73
+                    }
74
+                    return el;
75
+                }
76
+            });
77
+        } catch (Exception e) {
78
+            driver.quit();
79
+            throw e;
80
+        }
53 81
 
54 82
 //        webDriver.manage().timeouts().implicitlyWait(config.getWait(), TimeUnit.MILLISECONDS);
55 83
 
56 84
         //截取全屏
57
-        File f  = ((TakesScreenshot)webDriver).getScreenshotAs(OutputType.FILE);
85
+        File f  = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
58 86
 
59 87
         // 存入 oss
60 88
         String imageUrl = ossUtils.putObject(f);
89
+        log.info("Got image {}", imageUrl);
61 90
 
62 91
         //退出
63
-        webDriver.quit();
92
+        driver.quit();
64 93
 
65 94
         return imageUrl;
66 95
     }

+ 5
- 5
src/main/java/com/shigongli/controller/TaHouseController.java View File

@@ -234,11 +234,11 @@ public class TaHouseController extends BaseController {
234 234
         }
235 235
 
236 236
         String shot = null;
237
-//        try {
238
-//            shot = screenShotUtil.shot(String.format(weatherURL, city), "#weather-v2-plugin-standard");
239
-//        } catch (Exception e) {
240
-//            e.printStackTrace();
241
-//        }
237
+        try {
238
+            shot = screenShotUtil.shot(String.format(weatherURL, city));
239
+        } catch (Exception e) {
240
+            e.printStackTrace();
241
+        }
242 242
 
243 243
         Map<String, Object> res = new HashMap<>();
244 244
         res.put("image", shot);

+ 4
- 2
src/main/resources/application.yml View File

@@ -64,8 +64,10 @@ screen-shot:
64 64
   driver: /yunpan/shigongli/service/lib/chromedriver
65 65
   # milliseconds
66 66
   wait: 5000
67
-  width: '520'
68
-  height: '416'
67
+  #width: '520'
68
+  #height: '416'
69
+  width: '475'
70
+  height: '380'
69 71
 
70 72
 # weather-shot
71 73
 weather-shot: