我正在用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。
通過不刷新頁面,窗體保持與按下單選按鈕之前相同。通過這種形式,我無法找到要輸入的正確字段。在按下單選按鈕之後,我將如何獲取新表單以獲取新字段? – Ctech45
我快速瀏覽了一下頁面,看看發生了什麼。一旦單擊單選按鈕,該頁面將執行AJAX請求。您需要在點擊後執行等待,以便更新頁面。你可以看看這個[問題](http://stackoverflow.com/questions/17843521),看看我在說什麼 –
這是有道理的,在AJAX請求執行後,如果表單更新允許適當的領域被引用? – Ctech45