瀏覽代碼

解决chrome无法完全关闭的问题

xuzuoyun 5 年之前
父節點
當前提交
9c4cf9b321

+ 3 - 2
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/graphql/service/IKuaishouWebInterfaceService.java

@@ -1,12 +1,13 @@
 package cn.com.ctop.kuaishou.modules.graphql.service;
 
+import java.io.IOException;
 import java.util.Map;
 
 public interface IKuaishouWebInterfaceService {
-    public void getVideoList(String uid);
+    public void getVideoList(String uid) throws IOException;
     public String checkKuaishouUser(String videoUrl);
 
-    public void adkuaishouWebLogin(String phone, String password);
+    public void adkuaishouWebLogin(String phone, String password) throws IOException;
 
     public void deleteAllComment(Map<String, String> pcursorMap);
 }

+ 11 - 2
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/graphql/service/impl/KuaishouWebInterfaceServiceImpl.java

@@ -17,6 +17,7 @@ import org.openqa.selenium.By;
 import org.openqa.selenium.Cookie;
 import org.openqa.selenium.WebDriver;
 import org.openqa.selenium.chrome.ChromeDriver;
+import org.openqa.selenium.chrome.ChromeDriverService;
 import org.openqa.selenium.chrome.ChromeOptions;
 import org.openqa.selenium.support.ui.ExpectedConditions;
 import org.openqa.selenium.support.ui.WebDriverWait;
@@ -24,6 +25,8 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 
+import java.io.File;
+import java.io.IOException;
 import java.io.UnsupportedEncodingException;
 import java.net.URLEncoder;
 import java.text.SimpleDateFormat;
@@ -191,7 +194,9 @@ public class KuaishouWebInterfaceServiceImpl implements IKuaishouWebInterfaceSer
     private String chromeDriver;
 
     @Override
-    public void adkuaishouWebLogin(String phone, String password) {
+    public void adkuaishouWebLogin(String phone, String password) throws IOException {
+        ChromeDriverService service = new ChromeDriverService.Builder().usingDriverExecutable(new File(chromeDriver)).usingAnyFreePort().build();
+        service.start();
         System.getProperties().setProperty("webdriver.chrome.driver", chromeDriver);
         ChromeOptions chromeOptions = new ChromeOptions();
         WebDriver webDriver = new ChromeDriver(chromeOptions);
@@ -243,12 +248,15 @@ public class KuaishouWebInterfaceServiceImpl implements IKuaishouWebInterfaceSer
             e.printStackTrace();
         } finally {
             webDriver.quit();
+            service.stop();
 //            HttpUtils.cookieStore.clear();
         }
     }
 
     @Override
-    public void getVideoList(String uid) {
+    public void getVideoList(String uid) throws IOException {
+        ChromeDriverService service = new ChromeDriverService.Builder().usingDriverExecutable(new File(chromeDriver)).usingAnyFreePort().build();
+        service.start();
         System.getProperties().setProperty("webdriver.chrome.driver", chromeDriver);
         ChromeOptions chromeOptions = new ChromeOptions();
         WebDriver webDriver = new ChromeDriver(chromeOptions);
@@ -290,6 +298,7 @@ public class KuaishouWebInterfaceServiceImpl implements IKuaishouWebInterfaceSer
         } finally {
 //            HttpUtils.cookieStore.clear();
             webDriver.quit();
+            service.stop();
         }
     }