如何减少使用GhostDriver和PhantomJS从Web获取URL页面的时间 [英] How to reduce time in getting URL pages from web using GhostDriver and PhantomJS

查看:74
本文介绍了如何减少使用GhostDriver和PhantomJS从Web获取URL页面的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Maven中进行项目.我尝试从URl获取页面.到目前为止,我已经成功地从Web获取页面.但是我有两个问题,

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,

问题

  1. 下面的代码要花费大约14秒钟才能获取任意两个URL页面,如何减少此时间,请帮助我进行优化.
  2. 完成执行后,它不会从代码中退出.为什么 ? 我用driver.close()结束了代码.然后,为什么,它不能成功退出.我在开始之前和完成该过程之后都添加了快照.请看这些.
  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.

我的代码:-

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

开始该过程之前的快照,

该过程完成后的快照,当它无故等待时,

推荐答案

我怀疑驱动程序正在创建线程,但没有退出.尝试在main的末尾添加System.exit,看看它是否可以解决您的问题.

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.

这篇关于如何减少使用GhostDriver和PhantomJS从Web获取URL页面的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆