HTML Form manipulation is a fairly straight forward task with
WebRenderer. It involves using
the IBrowserCanvas method executeScript
which allows you to get direct
access through JavaScript to
HTML Forms on the page.
The
HTML Form manipulation example
textfield takes a String of
the text that you wish to enter
into the HTML textfield. There
is an "AutoForm" JButton
to add the Java textfield value
to the HTML form. There is also
a Submit Form JButton to submit
the HTML form.
//
Getting a reference to the documents html
form, getting reference to the elements
String javascript
= "var form=document.forms[0];"+"var
elms=form.elements;" + "elms.length;";
// Getting the number
of elements from elms.length for the html
form int num
= (new Integer(browser.executeScriptWithReturn(javascript))).intValue(); for
(int i=0; i< num; i++) { //
Loop through getting each elements name,
value and type javascript
= "elms["+i+"].name;"; String
name = browser.executeScriptWithReturn(javascript); javascript
= "elms["+i+"].value;"; String
value = browser.executeScriptWithReturn(javascript); javascript
= "elms["+i+"].type;"; String
type = browser.executeScriptWithReturn(javascript); System.out.println("For
Element "+i+" the Name is: "+name+"
and value is: "+value+" and type
is: "+type); //
If the type is a textfield we will set the
value to WebRenderer javascript
= "if (elms["+i+"].type=='text')elms["+i+"].value='My
Value for the HTML textfield';";
//
Executing the script on the browser to set
the textfield with the value browser.executeScriptWithReturn(javascript);
}
// Constructing
a form.submit JavaScript call String
javascript = "form.submit()"; //
Executing the JavaScript on the browser browser.executeScript(javascript);