How to reduce time in getting URL pages from web using GhostDriver and PhantomJS

雨燕双飞 提交于 2019-12-12 10:15:46

问题


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();


来源:https://stackoverflow.com/questions/15852687/how-to-reduce-time-in-getting-url-pages-from-web-using-ghostdriver-and-phantomjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!