问题描述:

I appreceate any little help I can get on the issue I'm facing with HtmlUnit.

What I need to do is to login to my application, go to the home page and get a list of all links, text fields, buttons, etc., on my home page.

I tried coding this, and here's what I can do.

  1. I can launch the page, find the login controls, enter user id and password and click Login.
  2. The login is successful. At this point, my application displays the home page, which is masked by an iFrame on top, which displays the last login time and other details. There's an OK button on that which I have to click for the underlying home page to be unmasked.
  3. I am able to click the OK button as well.

And now here's what I am unable to do.

Once I click OK, the underlying home page is unmasked, yet my HtmlPage object is empty (not null).

Below is my code.

import java.io.IOException;

import java.net.MalformedURLException;

import com.gargoylesoftware.htmlunit.Page;

import com.gargoylesoftware.htmlunit.WebClient;

import com.gargoylesoftware.htmlunit.WebResponse;

import com.gargoylesoftware.htmlunit.html.HtmlButton;

import com.gargoylesoftware.htmlunit.html.HtmlButtonInput;

import com.gargoylesoftware.htmlunit.html.HtmlForm;

import com.gargoylesoftware.htmlunit.html.HtmlPage;

import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;

public class HTMLUnitSample {

public static void main(String args[]){

final WebClient webClient = new WebClient();

webClient.getOptions().setJavaScriptEnabled(true);

webClient.getCookieManager().setCookiesEnabled(true);

webClient.getOptions().setThrowExceptionOnScriptError(false);

webClient.getOptions().setPopupBlockerEnabled(false);

try{

//Get the first page

final HtmlPage loginPage = webClient.getPage("http://****:****/****/****");

//Fill up the login form

loginPage.getElementById("USERID").setAttribute("value", "****");

loginPage.getElementById("user_pwd").setAttribute("value","****");

final HtmlForm loginForm = loginPage.getFormByName("frmLogin");

HtmlSubmitInput button = loginForm.getInputByName("submit");

HtmlPage homePage = button.click();

/*

System.out.println("Body - " + homePage.getBody().asText());

System.out.println("No. of Frames in Home Page - " + Inte ger.toString(homePage.getFrames().size()));

System.out.println("Total no. of elements on Home Page - " + Integer.toString(homePage.getElementsByTagName("*").size()));*/

//Click the OK button

HtmlButtonInput okButton = (HtmlButtonInput)homePage.getElementByName("Ok");

HtmlPage homePage2 = okButton.click();

System.out.println(homePage2.asText());

System.out.println("Done");

}

catch(MalformedURLException e){

e.printStackTrace();

}

catch(IOException e){

e.printStackTrace();

}

}

}

The output I get for

System.out.println(homePage2.asText());

is simply an empty string.

I even tried

homePage2.getElementsByTagName("*");

but in vain.

What I need is to actually find the elements on the home page after I click OK.

Please help me. Any little help is greatly appreciated.

相关阅读:
Top