How to reduce time in getting URL pages from web u

2019-06-05 17:22发布

问题:

I am doing project in Maven. I try to get pages from URl. Till now I am successful in getting pages from web. But I have two questions,

Qustions,

  1. Below code takes around 14 seconds to get any two URL pages, how can I reduce this time, Help me in optimizing this.
  2. After completing the execution, it does not exits from code. Why ? I ended the code with driver.close(). Then, why, it does not exits successfully. I added snapshots before starting and after completing the process. Please see these.

Help me in my problem. Please.

My code:-

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
    }
}

Snapshot before starting the process,

Snapshot after completing the process, when it waits for no reason,

回答1:

I suspect the driver is creating a thread and it did not exit. Try adding a System.exit at the end of main and see whether it solves your issue.



回答2:

You need to use

driver.quit();

instead of

driver.close();