|
try
{ IDocument
doc = browser.getDocument(); //Get
the Forms in the Document IElementCollection
forms = doc.getForms(); System.out.println("There
are " + forms.length() + " forms.");
//Go
through and find all the INPUTs for each
form //and
print thier name and value for
(int i = 0; i < forms.length(); i++)
{ System.out.println("<FORM
" + i + ">"); IElement
form = forms.item(i); Vector
inputs = new Vector();
//Call
a function that recursivly goes through
all //children
from an IElement and fills a Vector //with
children with the INPUT tag getInputElementsForForm(form,
inputs);
for
(int j = 0; j < inputs.size(); j++) { IElement
input = (IElement) inputs.elementAt(j); System.out.println(
"INPUT: name:"+ input.getAttribute("name",
0) + "\t value:" + input.getValue()); if(input.getValue().indexOf("Google
Search") != -1){ googleSearchBtn
= input; }
else if(input.getAttribute("name",
0).equals("q")){ //
Working on implementing/fixing setValue
and setAttribute bug //System.out.println("Got
the Q textfield"); //input.setValue("Set
within Java"); //System.out.println("getAttribute:
"+input.getAttribute("q",
0)); } } } }
catch (Exception ex) { System.out.println("Exception
caught: " + e); }
///////
public
void getInputElementsForForm(IElement at,
Vector items) { //If
the current element has the tagName INPUT,
add it to the Vector if
((at.getTagName() != null) && (at.getTagName().equalsIgnoreCase("input")))
{ items.add(at); }
else { //It's
not an Input so call this function on all
it's children IElementCollection
childs = at.getChildElements(); if
(childs != null) { for
(int i = 0; i < childs.length(); i++)
{ getInputElementsForForm(childs.item(i),
items); } }
} }
|