【问题标题】:How to get element by Xpath in HtmlUnit如何在 HtmlUnit 中通过 Xpath 获取元素
【发布时间】:2011-10-04 08:03:03
【问题描述】:

我正在尝试搜索亚马逊。我想选择类别,例如。书籍,键入一些搜索条件,例如。 java并单击Go按钮。我的问题是单击“开始”按钮。我有例外:

线程“主”java.lang.IndexOutOfBoundsException 中的异常:索引: 0,大小:0 在 java.util.ArrayList.rangeCheck(ArrayList.java:571) 在 java.util.ArrayList.get(ArrayList.java:349) 在 Bot.main(Bot.java:111) 处的 Bot.clickSubmitButton(Bot.java:77)

这是我的代码:

/**
 * @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(); 这是堆栈跟踪:

线程“main”中的异常 java.lang.NullPointerException at Bot.main(Bot.java:114) 处的 Bot.clickImageButton(Bot.java:81)

【问题讨论】:

    标签: java xpath htmlunit


    【解决方案1】:

    你试过了吗?

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

    我也建议你看看:getFirstByXPath

    【讨论】:

    • 我编辑了我的问题。你能给我一些选择这个图像输入的方法吗?
    • 如果 button.click() 触发 NullPointerException 那么 button 显然是空的。这意味着您没有正确获得它。但是,就我在亚马逊看到的代码而言,我提供的解决方案应该可以工作。尝试使用 System.out.println(currentPage.asXml()); 调试它因为您可能遗漏了其他内容(可能您不在您认为的网页中)
    • 我不知道如何,但现在我没有异常。谢谢。
    猜你喜欢
    • 2021-12-13
    • 2015-09-02
    • 2020-08-14
    • 2021-02-13
    • 1970-01-01
    • 2020-04-26
    • 1970-01-01
    • 1970-01-01
    • 2020-07-19
    相关资源
    最近更新 更多