4

I'm a newbie at this, basically I'm trying to use the HtmlUnitDriver, this is my code:

WebDriver driver = new HtmlUnitDriver();
driver.get("http://www.google.com");
System.out.println(driver.getPageSource());

But the page source I got is:

<?xml version="1.0" encoding="UTF-8"?>
<html>
  <head/>
  <body/>
</html>

I have tried to to new HtmlUnitDriver(true) but it's still not loading google I have already add the selenium server stand alone to the class path. Am I doing anything wrong? Thank you

P.S: Im using selenium-server-standalone-2.24.1.jar and jre 1.7

Mosty Mostacho
  • 42,742
  • 16
  • 96
  • 123
punkaceratop
  • 178
  • 1
  • 3
  • 10
  • Can't reproduce. I've just tested this exact code with Selenium 2.24.1 and got the whole page. – Petr Janeček Jun 28 '12 at 16:19
  • Im using selenium-server-standalone-2.24.1.jar and jre 1.7. So did I setup the project wrong or anything? – punkaceratop Jun 28 '12 at 16:31
  • I have selenium-java which is a subset of selenium-server-standalone... I really have no idea what could possible be wrong. Are you behind a proxy? Does it work in any other WebDriver? – Petr Janeček Jun 28 '12 at 16:32
  • yeah everything else works such as IE driver and Firefox driver, and I'm not sure about the proxy – punkaceratop Jun 28 '12 at 16:42
  • Okay, last shot. Try cleaning your classpath, delete all dependencies and re-download them again (Are you using Maven, Ivy, or anything similar? You might want to!). It _could_ be some [JAR-hell](http://en.wikipedia.org/wiki/JAR_hell)-related problem where some dependencies fight with each other. – Petr Janeček Jun 28 '12 at 16:55
  • 1
    I just tried that and still have the same problem. I also deleted the JRE library and added it back again. And Im not using Mavan or Ivy or anything. I only created a plain project from Eclipse – punkaceratop Jun 28 '12 at 17:47
  • Yeah. I'm out. We'll see whether someone can confirm this / propose a solution. In the meantime, try it on another computer. – Petr Janeček Jun 28 '12 at 18:05

2 Answers2

2

HtmlUnitDriver defaults to having javaScript disabled google.com relies heavily on javascript. try using driver.setJavascriptEnabled(true)

WebDriver driver = new HtmlUnitDriver();
driver.setJavascriptEnabled(true)
driver.get("http://www.google.com");
System.out.println(driver.getPageSource());
jkieley
  • 71
  • 1
  • 1
  • 4
0

The issue is definitely due to proxy missing while starting htmlunitdriver. You will have to provide proxy details

M_K
  • 119
  • 1
  • 3
  • 13