Java HtmlUnit form redirect issues
Asked Answered
H

1

6

I'm working in an online banking application. I would like to login to retrieve the balance. I've been doing some research, and I found some useful HtmlUnit code from other posts. However, I'm stuck handling page's redirect. The path is as follows : Main login form (page1), yields a new page that verify credentials (page2). Up to this point I'm doing fine, but I can't find the solution to retrieve home page (page3), here is the code :

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class WellsF {
    public static void main(String[] args) throws Exception {

        WebClient webClient = new WebClient(BrowserVersion.CHROME_16);
        webClient.getOptions().setJavaScriptEnabled(true);
        webClient.getOptions().setThrowExceptionOnScriptError(false);
        webClient.getOptions().setCssEnabled(true);
        webClient.getOptions().setUseInsecureSSL(true);
        webClient.getOptions().setRedirectEnabled(true);

        HtmlPage page1 = webClient
                .getPage("https://www.wellsfargo.com/home.jhtml");

        final HtmlForm form = (HtmlForm) page1.getElementById("frmSignon");

        form.getInputByName("userid").setValueAttribute("user id");
        form.getInputByName("password").setValueAttribute("user password");

        HtmlPage page2 = (HtmlPage) form.getInputByName("btnSignon").click();

        synchronized (page2) {
            page2.wait(5000);
            System.out.println(page2.asText());
            System.out.println("=========================== page2\n");
        }

        HtmlPage page3 = (HtmlPage) webClient.openWindow(page2.getUrl(),
                "signon").getEnclosedPage();
        System.out.println(page3.asText());
        webClient.closeAllWindows();
    }
}
Humerus answered 11/12, 2012 at 7:44 Comment(1)
This is probably about 2 months too late, however have you considered looking at webClient.getCurrentWindow().getEnclosedPage() to see if that is ok. I assume that you are having problems with redirects over anything else.Tobytobye
C
0

can you please try with below code and check if its working or not :

 HtmlPage page3 = (HtmlPage) webClient.getPage(page2.getUrl());
 webClient.getOptions().setTimeout(10*1000);
Countersubject answered 6/10, 2015 at 7:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.