我正在嘗試搜索Amazon。我想選擇類別,例如。書籍,鍵入一些搜索條件,例如。 java並單擊Go按鈕。我的問題是點擊開始按鈕。我有例外:如何在HtmlUnit中使用Xpath獲取元素
在線程異常 「主要」 java.lang.IndexOutOfBoundsException:指數: 0,大小:0在java.util.ArrayList.rangeCheck(ArrayList.java:571)在 的java .util.ArrayList.get(ArrayList.java:349)在 Bot.clickSubmitButton(Bot.java:77)在Bot.main(Bot.java:111)
這裏是我的代碼:
/**
* @author ivan.bisevac
*/
import java.io.IOException;
import java.net.MalformedURLException;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlImageInput;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlOption;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSelect;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
public class Bot {
private HtmlPage currentPage;
public HtmlPage getCurrentPage() {
return currentPage;
}
public Bot() {
}
/**
* Bot constructor
*
* @param pageAddress
* Address to go.
* @throws IOException
* @throws MalformedURLException
* @throws FailingHttpStatusCodeException
*/
public Bot(String pageAddress) throws FailingHttpStatusCodeException,
MalformedURLException, IOException {
this();
this.goToAddress(pageAddress);
}
/**
*
* @param pageAddress
* @throws FailingHttpStatusCodeException
* @throws MalformedURLException
* If pageAddress isn't formatted good (for example, it is just
* www.google.com without http://) then this exception is thrown
* @throws IOException
*/
public void goToAddress(String pageAddress)
throws FailingHttpStatusCodeException, MalformedURLException,
IOException {
WebClient webClient = new WebClient();
currentPage = webClient.getPage(pageAddress);
}
/**
* Fills text into input field
*
* @param inputId
* <input> tag id
* @param textValue
* Text to fill into input field
*/
public void setInputValue(String inputId, String textValue) {
HtmlInput input = (HtmlInput) currentPage.getElementById(inputId);
input.setValueAttribute(textValue);
}
/**
*
* @param buttonId
* Button id
* @throws IOException
*/
public void clickImageButton(String xpathExpr) throws IOException {
HtmlImageInput button = (HtmlImageInput) currentPage
.getFirstByXPath(xpathExpr);
currentPage = (HtmlPage) button.click();
}
/**
*
* @param radioButtonId
* @param radioButtonOption
* @throws IOException
* @throws InterruptedException
*/
public void selectRadioButton(String radioButtonId, String radioButtonOption)
throws IOException, InterruptedException {
final HtmlInput radio = (HtmlInput) currentPage
.getElementById(radioButtonId);
radio.click();
Thread.sleep(10000);
}
/**
*
* @param dropListId
* @param dropListOption
*/
public void selectDropList(String dropListId, String dropListOption) {
HtmlSelect select = (HtmlSelect) currentPage.getElementById(dropListId);
HtmlOption option = select.getOptionByValue(dropListOption);
select.setSelectedAttribute(option, true);
}
public static void main(String[] args) throws IOException {
Bot bot = new Bot("http://www.amazon.com");
bot.selectDropList("searchDropdownBox", "search-alias=stripbooks");
bot.setInputValue("twotabsearchtextbox", "java");
bot.clickImageButton("//div[@id='navGoButton']/input");
bot.getCurrentPage().getTitleText();
}
}
Obvoiusly在方法clickS中存在一些問題umbitButton,選擇div內的輸入元素。它給出了空陣列。有人會幫我解決這個問題嗎?編輯:重構方法clickImageButton後,我有錯誤行: currentPage =(HtmlPage)button.click(); 這裏是堆棧跟蹤:
在 Bot.clickImageButton(Bot.java:81)異常在線程 「主」 顯示java.lang.NullPointerException在Bot.main(Bot.java:114)
我編輯我的問題。你能建議我以某種方式選擇這個圖像輸入嗎? –
如果button.click()觸發NullPointerException,那麼按鈕顯然是空的。這意味着你沒有正確地得到它。然而就我在亞馬遜看到的代碼而言,我提供的解決方案應該可行。嘗試使用System.out.println(currentPage.asXml())調試它;因爲你可能會錯過別的東西(可能你不在你認爲你的網頁) –
我不知道如何,但現在我沒有例外。謝謝。 –