1

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,

enter image description here

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

enter image description here

Community
  • 1
  • 1
devsda
  • 4,112
  • 9
  • 50
  • 87

2 Answers2

2

You need to use

driver.quit();

instead of

driver.close();
Gus
  • 4,437
  • 2
  • 24
  • 27
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.

Dakshinamurthy Karra
  • 5,353
  • 1
  • 17
  • 28
  • It closes the program by adding `System.exit(0);`. But can you tell me why it not closes implicitly ? – devsda Apr 08 '13 at 06:09
  • I suspect the driver is creating a thread and it did not exit. – Dakshinamurthy Karra Apr 08 '13 at 08:29
  • When I saw my task manager, it has a lots of PhantomJS.exe actives. A very big problem before me. How can I overcome from this problem, in a right way ? – devsda Apr 08 '13 at 09:49
  • 2
    Try using driver.quit() instead of driver.close() – Dakshinamurthy Karra Apr 08 '13 at 10:31
  • Can you give me some guidance, how can I optimize this, as you said this is the best tool to get page dynamically. But time differential is too good. So, please tell me how can I optimize this either by increasing CPU or Memory space. – devsda Apr 08 '13 at 10:40
  • Did you time how long the URL actually takes to load? With FF and Ghost drivers? Just with wget/curl? – Dakshinamurthy Karra Apr 08 '13 at 10:42
  • FF takes around 14 - 19 seconds, and GhostDriver takes around 13 - 16 seconds. I tested on several cases to get two URLS dynamically and put them into String. wget and curl ? I a in Java. How can I use this in it , it is a linux command. – devsda Apr 08 '13 at 10:47
  • First of all thanks for your very nice support, now all the things works fine. But I have one last doubt, sometimes GhostDriver stucks To start this, it needs to restart the code again. But that is not suitable for me. Can you tell me why this weird behavior shows by GhostDriver. If this problem solves, then GhostDriver is the best for my case. I think you will surely give solution of this. Thanks again. – devsda Apr 09 '13 at 06:09