devsda
devsda

Reputation: 4222

HtmlUnit to get page source code implementation shows Exception

I try to get a dynamic page from URL. I am working in Java. I have done this using Selenium, but it takes lots of time. As it takes time to invoke driver of Selenium. That's why I shifted to HtmlUnit, as it is GUILess Browser. But my HtmlUnit implementation shows some exception.

Question :-

  1. How can I correct my HtmlUnit implementation.
  2. Is the page produced by Selenium is simiar to the page produced by HtmlUnit? [ Both are dynamic or not? ]

My selenium code is :-

 public static void main(String[] args) throws IOException {

 // Selenium
 WebDriver driver = new FirefoxDriver();
 driver.get("ANY URL HERE");  
 String html_content = driver.getPageSource();
 driver.close();

 // Jsoup makes DOM here by parsing HTML content
 Document doc = Jsoup.parse(html_content);

 // OPERATIONS USING DOM TREE

}

HtmlUnit code:-

package XXX.YYY.ZZZ.Template_Matching;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import org.junit.Assert;
import org.junit.Test;

public class HtmlUnit {

    public static void main(String[] args) throws Exception {
        //HtmlUnit htmlUnit = new  HtmlUnit();
        //htmlUnit.homePage();
        WebClient webClient = new WebClient();
        HtmlPage currentPage = webClient.getPage("http://www.jabong.com/women/clothing/womens-tops/?source=women-leftnav");
        String textSource = currentPage.asText();
        System.out.println(textSource);
    }
}

It shows exception :-

enter image description here

Upvotes: 0

Views: 1505

Answers (1)

Stephen C
Stephen C

Reputation: 718826

1: How can I correct my HtmlUnit implaementation.

Looking at the stack trace, it seems to be saying that the javascript engine executed some javascript that tried to access an attribute on a Javascript "undefined" value. If it is correct, that would be a bug in the javascript you are testing, not in the HtmlUnit code.

2: Is the page produced by Selenium is simiar to the page produced by HtmlUnit?

That does not make sense. Neither Selenium or HtmlUnit "produces" a page. The page is produced by the serve code you are testing.

If you are asking if HtmlUnit is capable of dealing with code that has embedded Javascript ... there is clear evidence in the stacktrace that it is trying to execute the Javascript.

Upvotes: 1

Related Questions