2013-11-20 168 views
0

我正在用Java編寫一個程序,該程序有一個單選按鈕,需要單擊以填寫一組信息。我目前遇到問題,找到單擊單選按鈕後需要輸入的字段。目前,我的代碼是:單選按鈕問題

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"); 
} 

雖然第一頁是需要被繞過安全頁,第二頁是在那裏我遇到了這個問題,因爲我得到的錯誤「

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)" 

我認爲這意味着輸入字段無法在表單中找到我一直在引用兩個網站作爲參考Website1,Website2我不確定,但我相信在設置收音機後我可能需要創建一個新的HtmlPage按鈕變爲true。

回答

0

不知道頁面是不可能看到錯誤發生的原因。然而,正如你所說,很顯然getInputByName沒有找到元素並引發異常。

鑑於代碼,並假設你沒有在字符串中犯了語法錯誤取的名字輸入,我建議刪除此行:

page2.refresh(); 

修改程序後刷新頁面可能會導致再次獲取未修改的頁面。

關於在將單選按鈕設置爲true後創建新的HtmlPage,只有當收音機有onchange或附加的類似事件觸發JavaScript AJAX調用才能修改DOM並創建您所在的元素試圖獲取。

這就是我可以建議給定的代碼。

+0

通過不刷新頁面,窗體保持與按下單選按鈕之前相同。通過這種形式,我無法找到要輸入的正確字段。在按下單選按鈕之後,我將如何獲取新表單以獲取新字段? – Ctech45

+0

我快速瀏覽了一下頁面,看看發生了什麼。一旦單擊單選按鈕,該頁面將執行AJAX請求。您需要在點擊後執行等待,以便更新頁面。你可以看看這個[問題](http://stackoverflow.com/questions/17843521),看看我在說什麼 –

+0

這是有道理的,在AJAX請求執行後,如果表單更新允許適當的領域被引用? – Ctech45

0

在創建page2之後的代碼中,您將創建一個WebRequest而不是像這樣創建一個新頁面。

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); 

上面的代碼成功完成後,您得到一個字符串,其中您的響應來了。