瀏覽代碼

Merge remote-tracking branch 'origin/master' into master

zlisen 4 年之前
父節點
當前提交
474d4ec857

+ 17
- 6
src/main/java/com/shigongli/common/ScreenShotUtil.java 查看文件

@@ -1,11 +1,11 @@
1 1
 package com.shigongli.common;
2 2
 
3 3
 import com.shigongli.config.ScreenShotConfig;
4
-import org.openqa.selenium.OutputType;
5
-import org.openqa.selenium.TakesScreenshot;
6
-import org.openqa.selenium.WebDriver;
4
+import org.openqa.selenium.*;
7 5
 import org.openqa.selenium.chrome.ChromeDriver;
8 6
 import org.openqa.selenium.chrome.ChromeOptions;
7
+import org.openqa.selenium.support.ui.ExpectedCondition;
8
+import org.openqa.selenium.support.ui.WebDriverWait;
9 9
 import org.springframework.beans.factory.annotation.Autowired;
10 10
 import org.springframework.stereotype.Component;
11 11
 
@@ -20,7 +20,7 @@ public class ScreenShotUtil {
20 20
     @Autowired
21 21
     ScreenShotConfig config;
22 22
 
23
-    public String shot(String url) throws Exception {
23
+    public String shot(String url, String cssSelector) throws Exception {
24 24
         //驱动地址
25 25
         System.setProperty("webdriver.chrome.driver", config.getDriver());
26 26
 
@@ -39,8 +39,19 @@ public class ScreenShotUtil {
39 39
 
40 40
         //访问网址
41 41
         webDriver.get(url);
42
-//        Thread.sleep(config.getWait());
43
-        webDriver.manage().timeouts().implicitlyWait(config.getWait(), TimeUnit.MILLISECONDS);
42
+
43
+        // 设置超时等待
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
+        });
53
+
54
+//        webDriver.manage().timeouts().implicitlyWait(config.getWait(), TimeUnit.MILLISECONDS);
44 55
 
45 56
         //截取全屏
46 57
         File f  = ((TakesScreenshot)webDriver).getScreenshotAs(OutputType.FILE);

+ 1
- 1
src/main/java/com/shigongli/controller/TaHouseController.java 查看文件

@@ -211,7 +211,7 @@ public class TaHouseController extends BaseController {
211 211
 
212 212
         String shot = null;
213 213
         try {
214
-            shot = screenShotUtil.shot(String.format(weatherURL, city));
214
+            shot = screenShotUtil.shot(String.format(weatherURL, city), "#weather-v2-plugin-standard");
215 215
         } catch (Exception e) {
216 216
             e.printStackTrace();
217 217
         }