2013-04-06 38 views
1

我在做項目的Maven從網頁URL的網頁。我嘗試從URl獲取頁面。直到現在我成功地從網頁上獲取網頁。但我有兩個問題,如何減少時間獲得使用GhostDriver和PhantomJS

Qustions

  1. 下面的代碼需要約14秒,以獲得任何兩個URL的網頁,我怎麼能縮短這個時間,幫我優化這一點。
  2. 完成執行後,它不會從代碼退出。爲什麼? 我以driver.close()結束了代碼。那麼,爲什麼呢,它不能成功退出。我在開始之前和完成過程之後添加了快照。請看這些。

幫助我在我的問題。請。

我的代碼: -

package XXX.YYY.ZZZ.Template_Matching; 

import com.gargoylesoftware.htmlunit.WebClient; 
import com.gargoylesoftware.htmlunit.html.HtmlPage; 
import java.io.IOException; 
import org.junit.Assert; 
import org.junit.Test; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.phantomjs.PhantomJSDriver; 
import org.openqa.selenium.phantomjs.PhantomJSDriverService; 
import org.openqa.selenium.remote.DesiredCapabilities; 
import org.w3c.dom.Document; 

public class HtmlUnit { 

    public static void main(String[] args) throws Exception { 
     String url1 = "http://www.jabong.com/men/shoes/men-loafers/?source=home-leftnav"; 
     String url2 = "http://www.jabong.com/fastrack-9915Pp36J-Black-Pink-Analog-Watch-198499.html"; 
     DesiredCapabilities caps = new DesiredCapabilities(); 
     caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "C://Users//jhamb//Desktop//phantomjs-1.9.0-windows//phantomjs.exe"); 
     WebDriver driver = new PhantomJSDriver(caps); 
     driver.get(url1); 
     String hml1 = driver.getPageSource(); 
     driver.get(url2); 
     String hml2 = driver.getPageSource(); 
     driver.close(); 
     //System.out.println(hml1); 
     //System.out.println(hml2); 
      Document doc1 = Jsoup.parse(hml1); 
      Document doc2 = Jsoup.parse(hml2); 
      // Some operations using these DOM tree, just like , comparing Templates of two URLS 
    } 
} 

啓動過程之前快照,

enter image description here

完成過程中,當等待沒有理由後快照,

enter image description here

回答

1

我懷疑司機是創建一個線程,它並沒有退出。嘗試在main的結尾添加一個System.exit,看它是否解決了你的問題。

+0

它關閉該程序(0);'。但你能告訴我爲什麼它不隱式關閉? – devsda 2013-04-08 06:09:09

+0

請幫幫我。 – devsda 2013-04-08 08:24:17

+0

我懷疑驅動程序正在創建一個線程,並沒有退出。 – KDM 2013-04-08 08:29:22

2

您需要通過添加`System.exit使用的

driver.quit(); 

代替

driver.close();