During a set of interactions with a web site (that I do not control), I'm able to get a reference to a form and am able to select and enter data on the form, as confirmed by examination of the DOM, but I've tried several ways to submit, without joy.
There are many SO questions about how to submit a form with HtmlUnit, but I feel this question is not yet addressed because what I have discovered is that the page in question, after clicking a "button" in Firefox, goes right into minified jquery code, which eventually performs a dispatch. I'm wondering how I can cause the same thing to happen in HtmlUnit.
Some of the more interesting things on the page in question look like this:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
...
...
<form method="POST" action="" class="form login-form text-center">
...
...
<input type="submit" name="submit" value="GO" class="form-control btn-warning">
</form>
When "GO" is clicked in Firefox, the Network interactions show a 302 followed by a 200 status, and the next page is displayed.
The first things I tried were the obvious:
form.getInputByName("submit").click(); //No network activity
and
form.submit(form.getInputByName("submit")); //No network activity
and even
DomElement newButton = myPage.createElement("aNewButton");
newButton.setAttribute("type","submit");
form.appendChild(newButton);
newButton.click(); //No network activity
With help from someone on StackOverflow, I tried:
// Execute Javascript
ScriptResult scriptResult = page.executeJavaScript("$('form.login-form')[0].submit()");
webClient.waitForBackgroundJavaScript(5000);
if (scriptResult == null || ScriptResult.isUndefined(scriptResult)) {
Log.v(TAG, "ERROR: scriptResult " + scriptResult);
} else {
Object whatIsThis = scriptResult.getJavaScriptResult();
if (whatIsThis == null) Log.v(TAG, "No object returned.");
else Log.v(TAG, "Objec type returned:" + whatIsThis.getClass().getName());
}
The above says No object returned.
I tried it with \"form.login-form\"
with the same result.
When "GO" is clicked, the debugger in Firefox goes to this portion of the jquery code:
w.event = {
global: {},
add: function(e, t, n, r, i) {
var o, a, s, u, l, c, f, p, d, h, g, y = J.get(e);
if (y) {
n.handler && (n = (o = n).handler, i = o.selector), i && w.find.matchesSelector(be, i), n.guid || (n.guid = w.guid++), (u = y.events) || (u = y.events = {}), (a = y.handle) || (a = y.handle = function(t) {
return "undefined" != typeof w && w.event.triggered !== t.type ? w.event.dispatch.apply(e, arguments) : void 0
}), l = (t = (t || "").match(M) || [""]).length;
while (l--) d = g = (s = Ce.exec(t[l]) || [])[1], h = (s[2] || "").split(".").sort(), d && (f = w.event.special[d] || {}, d = (i ? f.delegateType : f.bindType) || d, f = w.event.special[d] || {}, c = w.extend({
type: d,
origType: g,
data: r,
handler: n,
guid: n.guid,
selector: i,
needsContext: i && w.expr.match.needsContext.test(i),
namespace: h.join(".")
}, o), (p = u[d]) || ((p = u[d] = []).delegateCount = 0, f.setup && !1 !== f.setup.call(e, r, h, a) || e.addEventListener && e.addEventListener(d, a)), f.add && (f.add.call(e, c), c.handler.guid || (c.handler.guid = n.guid)), i ? p.splice(p.delegateCount++, 0, c) : p.push(c), w.event.global[d] = !0)
}
},
More specifically here:
(a = y.handle = function(t) {
return "undefined" != typeof w && w.event.triggered !== t.type ? w.event.dispatch.apply(e, arguments) : void 0
})
At this point, I'm completely in the weeds. I know there's got to be some kind of dispatch going on.
Note that I can see (through the Network interactions) exactly what the next interaction with the server is. If I weren't trying to use HtmlUnit's abilities (instead just what I used to do with HttpClient), I would build-up the request with all of the appropriate fields and submit it "manually". But this question isn't about how to get this interaction done some other way. It's about how to discover what to use in the HtmlUnit code to emulate what the browser does.
The JavaScript exception is as follows:
EcmaError: lineNumber=[1] column=[0] lineSource=[<no source>] name=[TypeError]
sourceName=[injected script] message=[TypeError: Cannot call property submit in object [object HTMLFormElement].
It is not a function, it is "object". (injected script#1)]
com.gargoylesoftware.htmlunit.ScriptException: TypeError: Cannot call property submit
in object [object HTMLFormElement].
It is not a function, it is "object". (injected script#1)
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too