An alternative that allows me to fill an HTML form that has checkboxes and radiobuttons.
I was creating this android app that asks user input and sends that data to a website with an html form, fills it, submits the form, and returns the following results page.
I already managed to send data to the html form and retrieve the page using the HtmlUnit library in eclipse (I have posted the Java code for that below).
However, when I copied that code to my Android project I found out that Android does not support the HtmlUnit library.
Is there another alternative to HtmlUnit for Android? The alternative should be able to fill in texts, checkboxes, radiobuttons into an Html form and click on the submit button
Html form code:
<form method="post" action="https://www.xxxxx.com/cgi-bin/xxxxxx.cgi">
<p><em>Person:</em>
<input size="18" name="name"><br>
<input type="radio" name="login" value="no" checked="">Name <input type="radio" name="login" value="yes">Username</p>
<p><em>Title:</em>
<input size="18" name="title"></p>
<p><em>Department:</em>
<input size="18" name="department"></p>
<p><em>Groups to Search:</em><br>
<input type="checkbox" name="get_student" value="yes" checked=""> Students<br>
<input type="checkbox" name="get_alum" value="yes" checked=""> Alumni<br>
<input type="checkbox" name="get_staff" value="yes" checked=""> Staff<br>
<input type="checkbox" name="get_faculty" value="yes" checked=""> Faculty</p>
<p><input type="submit" value="Search"></p>
</form>
HtmlUnit Java Code:
public static String submittingForm() throws Exception {
final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_38);
webClient.getOptions().setJavaScriptEnabled(false);
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
WebRequest request = new WebRequest(new URL("https://www.xxxxx.com/"));
// Get the first page
HtmlPage page1 = webClient.getPage(request);
System.out.println("PULLING LINKS/ LOADING:");
// Get the form that we are dealing with and within that form,
// find the submit button and the field that we want to change.
List<HtmlForm> listform = page1.getForms();
HtmlForm form = listform.get(0);
HtmlElement Name = page1.getElementByName("name");
Name.click();
Name.type("Adonay");
HtmlElement nameRadio = page1.getFirstByXPath("/html/body//div[@id='wrapper']//div[@id='layout']//div[@id='container']//div[@id='col1']//div[@id='content']//div[@class='directory-search']//form//input[@type='radio' and @value='no']");
HtmlElement userRadio = page1.getFirstByXPath("/html/body//div[@id='wrapper']//div[@id='layout']//div[@id='container']//div[@id='col1']//div[@id='content']//div[@class='directory-search']//form//input[@type='radio' and @value='yes']");
/* userRadio.click(); click when username wanted*/
HtmlElement Title = page1.getElementByName("title");
Title.click();
Title.type("");
HtmlElement Department = page1.getElementByName("department");
Department.click();
Department.type("");
HtmlElement studentBox = page1.getFirstByXPath("/html/body//div[@id='wrapper']//div[@id='layout']//div[@id='container']//div[@id='col1']//div[@id='content']//div[@class='directory-search']//form//input[@type='checkbox' and @name='get_student']");
studentBox.click();
//add clicker here
HtmlElement alumniBox = page1.getFirstByXPath("/html/body//div[@id='wrapper']//div[@id='layout']//div[@id='container']//div[@id='col1']//div[@id='content']//div[@class='directory-search']//form//input[@type='checkbox' and @name='get_alum']");
alumniBox.click();
//add clicker here
HtmlElement staffBox = page1.getFirstByXPath("/html/body//div[@id='wrapper']//div[@id='layout']//div[@id='container']//div[@id='col1']//div[@id='content']//div[@class='directory-search']//form//input[@type='checkbox' and @name='get_staff']");
staffBox.click();
//add clicker here
HtmlElement facultyBox = page1.getFirstByXPath("/html/body//div[@id='wrapper']//div[@id='layout']//div[@id='container']//div[@id='col1']//div[@id='content']//div[@class='directory-search']//form//input[@type='checkbox' and @name='get_faculty']");
facultyBox.click();
//add clicker here
HtmlElement button = page1.getFirstByXPath("/html/body//div[@id='wrapper']//div[@id='layout']//div[@id='container']//div[@id='col1']//div[@id='content']//div[@class='directory-search']//form//input[@type='submit' and @value='Search']");
// Change the value of the text field
// Now submit the form by clicking the button and get back the second page.
HtmlPage page2 = button.click();
webClient.waitForBackgroundJavaScript(200);
return(page2.asXml());
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…