0

I am writing a program in Java using Htmlunit that has a Radio Button that needs to be clicked to fill out a set of information. I am currently having an issue finding the fields that need to be entered after the radio button is clicked. Currently my code is:

String url = "http://cpdocket.cp.cuyahogacounty.us/";

final WebClient webClient = new WebClient();
final HtmlPage page = webClient.getPage(url);
final HtmlForm form = page.getForms().get(0);
final HtmlElement button = form.getElementById("SheetContentPlaceHolder_btnYes");
final HtmlPage page2 = button.click();
try {
  synchronized (page2) {
    page2.wait(3000);
  }
}
catch(InterruptedException e)
{
  System.out.println("error");
}

//returns the first page after the security page
final HtmlForm form2 = page2.getForms().get(0);
final HtmlRadioButtonInput button2 = form2.getInputByValue("forcl");
button2.setDefaultChecked(true);
page2.refresh();
final HtmlForm form3 = page2.getForms().get(0);
form3.getInputByName("ctl00$SheetContentPlaceHolder$foreclosureSearch$txtZip").setValueAttribute("44106");
final HtmlSubmitInput button3 = form3.getInputByValue("Submit");
final HtmlPage page3 = button3.click();
try {
  synchronized (page3) {
    page2.wait(10000);
  }
}
catch(InterruptedException e)
{
  System.out.println("error");
}

While the first page is a security page that needs to be bypassed, the second page is where I am running into the issue as I am getting the error "

com.gargoylesoftware.htmlunit.ElementNotFoundException: elementName=[input] attributeName=[name] attributeValue=[ctl00$SheetContentPlaceHolder$foreclosureSearch$txtZip]
    at com.gargoylesoftware.htmlunit.html.HtmlForm.getInputByName(HtmlForm.java:463)
    at Courtscraper.scrapeWebsite(Courtscraper.java:58)"

I believe this means that the input field cannot be found in the form. I have been referring to two websites as reference. Website1, Website2. I am not sure, but i believe I may have to create a new HtmlPage after setting the radio button to true.

Community
  • 1
  • 1
Ctech45
  • 496
  • 9
  • 17

2 Answers2

0

Without knowing the page it is impossible to see why the error is happening. However, as you say, it is clear that the getInputByName is not finding the element and raising the exception.

Given that code, and assuming you've not committed a syntactical error in the string to fetch the input by name, I would suggest removing this line:

page2.refresh();

Refreshing the page after making modifications to it might result in getting an unmodified page again.

Regarding creating a new HtmlPage after setting the radio button to true, that would only be necessary if the radio has an onchange or a similar event attached that fires a JavaScript AJAX call that modifies the DOM and creates the element that you are trying to fetch.

That's all I can suggest given that code.

Mosty Mostacho
  • 42,742
  • 16
  • 96
  • 123
  • By not refreshing the page, the form stays the same as before the radiobutton was pressed. Through this form, I am not able to find the correct fields to type into. How would I go about retrieving the new form to obtain the new fields after the radiobutton have been pressed? – Ctech45 Dec 04 '13 at 20:59
  • I took a quick look at the page and I see what is going on. That page performs an AJAX request once a radio button is clicked. You will need to perform a wait after it is clicked so that the page gets updated. You can take a look at this [question](http://stackoverflow.com/questions/17843521) to see what I'm talking about – Mosty Mostacho Dec 04 '13 at 21:21
  • That makes sense, after the AJAX request is performed, should the form update allowing for the proper fields to be referenced? – Ctech45 Dec 04 '13 at 21:23
  • It should, unless the JavaScript code throws one of those infamous HTMLUnit's `ScriptException`s :) – Mosty Mostacho Dec 04 '13 at 21:30
0

In your code after creating page2 you will make a WebRequest not creating a new page like this.

String url = "http://cpdocket.cp.cuyahogacounty.us/Search.aspx";
String EventTarget = "ctl00$SheetContentPlaceHolder$rbCivilForeclosure";
String world = "ctl00$SheetContentPlaceHolder$UpdatePanel1|ctl00$SheetContentPlaceHolder$rbCivilForeclosure";
String Viewstate = page2.getElementById("__VIEWSTATE").getAttribute("value");
String EventValidation = page2.getElementById("__EVENTVALIDATION").getAttribute("value");

WebRequest req1 = new WebRequest(new URL(url));
req1.setHttpMethod(HttpMethod.POST);
req1.setAdditionalHeader("Origin", "http://cpdocket.cp.cuyahogacounty.us");
req1.setAdditionalHeader("Referer", "http://cpdocket.cp.cuyahogacounty.us/Search.aspx");
req1.setAdditionalHeader("X-Requested-With", "XMLHttpRequest");

String txtview1 = "ctl00$ScriptManager1=" + URLEncoder.encode(world) + "&__EVENTTARGET=" + URLEncoder.encode(EventTarget) + "&__EVENTARGUMENT=&__LASTFOCUS=&__VIEWSTATE=" + URLEncoder.encode(Viewstate) + "&__EVENTVALIDATION=" + URLEncoder.encode(EventValidation) + "&ctl00$SheetContentPlaceHolder$rbSearches=forcl&__ASYNCPOST=true&";
//System.out.println("this is text view =============== " + txtview1);
req1.setRequestBody(txtview1);
req1.setAdditionalHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
String re=client.getPage(req1).getWebResponse().getContentAsString();
System.out.println("========== " + re);

After done above code successfully you getting a String in which your response is come.

nyg
  • 2,380
  • 3
  • 25
  • 40