1

I have a problem with HTMLUnit. Shortly what I'm doing is, I'm filling a form and I log-in to a webpage, then I press a button on that page. Actually, I can't do this process, but I'm trying. Here's my HTML form source codes and Java source code:

This is from the login screen:

<form action="/login" method="post"> 
 ...
 <input type="text" name="login_email" id="login_email" value="" />
 <input type="password" name="login_password" id="login_password" />
 <input type="submit" id="login_submit" name="login_submit" value="Sign in" />
</form>

There are some hidden inputs in this form. I know it sounds funny, but my Java code works when I don't do anything about hidden inputs.

Here's my Java code for logging in using this form:

This code is from a stackoverflow question. I'm just testing it, nothing more.

WebClient webClient = new WebClient();
webClient.setThrowExceptionOnScriptError(false);

HtmlPage currentPage = webClient.getPage("https://www.blablabla.com:1234");
final HtmlForm form = currentPage.getFirstByXPath("//form[@action='/login']");
HtmlTextInput username = (HtmlTextInput) currentPage.getElementById("login_email");
HtmlPasswordInput password = (HtmlPasswordInput) currentPage.getElementById("login_password");

username.setText("[email protected]");
password.setText("passW0rd");
HtmlButton submitButton = (HtmlButton) currentPage.createElement("button");
submitButton.setAttribute("type", "submit");
form.appendChild(submitButton);

HtmlPage newPage = submitButton.click();

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

Things are good until the next part. I can login, see the contents of the new page.

However, when I try to press the button in the new page, I get nothing. Actually, I can't even press it I guess.

Here's the HTML source of my "buttony" and new webpage:

<form action="auth" method="post">
 <input type="submit" name="allow" value="Allow"/>
</form>

There are some hidden inputs as well.

Here's the Java code for -trying- to press the button with the name 'allow':

HtmlButton button = newPage.getElementByName("allow");
HtmlPage page = button.click() ;

And to check things for the last time, I use another piece of code:

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

But I get errors like these

errors start

WARNING: getElementById(script1338426904717) did a getElementByName for Internet Explorer
31.May.2012 04:15:04 com.gargoylesoftware.htmlunit.javascript.host.ActiveXObject jsConstructor
WARNING: Automation server can't create object for 'ShockwaveFlash.ShockwaveFlash'.
31.May.2012 04:15:04 com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter     runtimeError
SEVERE: runtimeError: message=[Automation server can't create object for     'ShockwaveFlash.ShockwaveFlash'.] sourceName=[https://www.jdkahsjkda/dksajda.js] line=[12]     lineSource=[null] lineOffset=[0]
31.May.2012 04:15:04 com.gargoylesoftware.htmlunit.javascript.host.ActiveXObject     jsConstructor

end of errors

These errors are OK for me as long as I can login.

I can login, and see the page. It says things like "Welcome username password..." But, I can't press the button nor do anything else.

I hope you guys can help me with this problem.

Thank you very much.

Take care, and thank you.

Edit:

Now I get this error:

Exception in thread "main" com.gargoylesoftware.htmlunit.ElementNotFoundException: elementName=[*] attributeName=[name] attributeValue=[allow]
at com.gargoylesoftware.htmlunit.html.HtmlPage.getElementByName(HtmlPage.java:1565)
at cza.main(cza.java:54)

However, there's a button called 'allow'. I'm looking at the source of the second page, and I see this:

<input type="submit" name="allow" value="Allow"/>
<input type="submit" name="deny" value="Deny"/>

So, there's a button named as allow and deny. However, this code fails. Can this be because of JS or anything? I tried finding the submit button from firstPage and submit the form using it. Not with the fake button, it fails again. I used HTMLSubmitInput for this, it fails again.

Thanks again.

1 Answer 1

2

Sorry I can't comment yet...

What's the newPage.getWebResponse().getContentAsString() content ? I guess your page may contains many html elements with name "allow"

It's best to be sure that you get an unique and right element, there's many ways to do it :

element.getElementById("id")
page.getFirstByXPath("xpathExpr") || page.getByXPath("xpathExpr")

and so on ... the goal is really to be sure to use the element you need.

When playing with inputs, it's always a good way to get as variable the form to manipulate the inputs. eg :

HtmlForm form = page.getforms(0); 
form.getInputByName("name");
form.getInputByValue("value");

BTW, some tips : initialize the webclient with FF settings like this : client = new WebClient(BrowserVersion.FIREFOX_3_6); it got the best html code coverage ( http://build.canoo.com/htmlunit/artifacts/ )

always try with JS on/off : client.setJavaScriptEnabled(false);client.setThrowExceptionOnScriptError(false);

Sign up to request clarification or add additional context in comments.

2 Comments

I updated my code. Problems are still present. However, I'll change them with your feedback. You can look at my update this far here. Thank you very much. gist.github.com/2853917
I changed code like this: HtmlForm form2 = newPage.getFirstByXPath("//form[@action='authorize']"); HtmlPage finalPage = form2.getButtonByName("allow").click(); Yet it still fails.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.