Hi all,
I am kinda new to HtmlUnit and am having some trouble getting a form to submit with HtmlImageInput.click(). When I call the method, nothing seems to happen, no form submission, no round trip to the server, or anything, as far as I can tell. The method returns immediately, returning the current page.
There's no Javascript event handler attached to the image input. It's just a plain old vanilla image input, nothing special going on. The input is initially set to disabled when the page is loaded, and then gets enabled as the user interacts with certain AJAXy elements within the page. But by the time I click on the input, it has already been enabled, so I don't think it's an AJAX issue.
Anybody have an idea of what is going on? Runnable source code pasted below.
Thanks, Matthew
import java.io.*;
import java.util.*;
import com.gargoylesoftware.htmlunit.*;
import com.gargoylesoftware.htmlunit.html.*;
import org.w3c.dom.*;
public class Test {
public static void main(String args[]) {
try {
WebClient webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER_7);
webClient.setThrowExceptionOnScriptError(false);
HtmlPage page = webClient.getPage("http://us.megabus.com");
System.out.println("got the page");
HtmlForm form = page.getFormByName("ctl01");
System.out.println("got the form");
HtmlSelect select = form.getSelectByName("SearchAndBuy1$ddlLeavingFrom");
select.click();
System.out.println("clicked the select");
HtmlOption option = select.getOptionByValue("13");
option.click();
System.out.println("clicked the option...going to sleep");
try { Thread.sleep(15000); } catch(InterruptedException e) {}
select = form.getSelectByName("SearchAndBuy1$ddlTravellingTo");
select.click();
System.out.println("clicked the select 2");
option = select.getOptionByValue("37");
option.click();
System.out.println("clicked the option 2...going to sleep");
try { Thread.sleep(15000); } catch(InterruptedException e) {}
HtmlImage image = (HtmlImage)page.getElementById("SearchAndBuy1_imgOutboundDate");
image.click();
System.out.println("clicked the image");
String month = "April";
String date = "09";
HtmlTable table = (HtmlTable)page.getElementById("SearchAndBuy1_calendarOutboundDate");
HtmlTableRow row = ((HtmlTable)table.getCellAt(0, 0).getChildElements().iterator().next()).getRow(0);
String monthString = row.getCell(1).getTextContent();
monthString = monthString.substring(0, monthString.indexOf(' '));
while(!monthString.equals(month)) {
row.getCell(2).getChildElements().iterator().next().click();
System.out.println("clicked to go to the next month");
try { Thread.sleep(15000); } catch(InterruptedException e) {}
table = (HtmlTable)page.getElementById("SearchAndBuy1_calendarOutboundDate");
row = ((HtmlTable)table.getCellAt(0, 0).getChildElements().iterator().next()).getRow(0);
monthString = row.getCell(1).getTextContent();
monthString = monthString.substring(0, monthString.indexOf(' '));
}
DomNodeList<HtmlElement> aList = table.getElementsByTagName("a");
for (int i = 0; i < aList.size(); i++) {
HtmlAnchor anchor = (HtmlAnchor)aList.get(i);
if (anchor.getAttribute("title").equals(DomElement.ATTRIBUTE_NOT_DEFINED) || anchor.getAttribute("title").equals(DomElement.ATTRIBUTE_VALUE_EMPTY))
throw new RuntimeException("DomElement ATTRIBUTE_NOT_DEFINED or ATTRIBUTE_VALUE_EMPTY");
if (anchor.getAttribute("title").equals(month + " " + date)) {
anchor.click();
try { Thread.sleep(15000); } catch(InterruptedException e) {}
break;
}
}
HtmlImageInput imageInput = (HtmlImageInput)page.getElementByName("SearchAndBuy1$btnSearch");
page = (HtmlPage)imageInput.click();
System.out.println("clicked search button");
} catch(FailingHttpStatusCodeException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
} catch(ElementNotFoundException e) {
e.printStackTrace();
} catch(IndexOutOfBoundsException e) {
e.printStackTrace();
}
}
}