java - HtmlUnit中如何通过Xpath获取元素

标签 java xpath htmlunit

我正在尝试搜索亚马逊。我想选择类别,例如。书籍,输入一些搜索条件,例如。 java 并单击 Go 按钮。我的问题是单击 Go 按钮。我有异常(exception):

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:571) at java.util.ArrayList.get(ArrayList.java:349) at Bot.clickSubmitButton(Bot.java:77) at 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();
    }
}

显然,clickSumbitButton 方法在选择 div 中的输入元素时存在一些问题。它给出空数组。有人可以帮我解决这个问题吗?

编辑:重构方法clickImageButton后,在线报错: currentPage = (HtmlPage) button.click(); 这是堆栈跟踪:

Exception in thread "main" java.lang.NullPointerException at Bot.clickImageButton(Bot.java:81) at Bot.main(Bot.java:114)

最佳答案

你试过吗?

bot.clickSubmitButton("//div[@id='navGoButton']/input");

我还建议您看一下:getFirstByXPath

关于java - HtmlUnit中如何通过Xpath获取元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7641885/

相关文章:

java - 在 Java 程序之间共享 I/O

xpath - 从@href属性获取值

Java:无法关闭 HtmlUnit 日志消息(2016)

.net - HtmlUnit 不等待 AJAX 执行

java - 检查字符串中是否存在模式

java - 使用 Json-simple 从文件中解析对象数组

java - hadoop/emr如何存储键值对

html - 检查HTML是否通过xpath,Selenium IDE包含文本

selenium - Xpath 有效,可以在 Elements 和 Console 中找到,但在运行应用程序时找不到

java - HtmlUnit 按钮点击