Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在HtmlUnit中通过Xpath获取元素_Java_Xpath_Htmlunit - Fatal编程技术网

Java 如何在HtmlUnit中通过Xpath获取元素

Java 如何在HtmlUnit中通过Xpath获取元素,java,xpath,htmlunit,Java,Xpath,Htmlunit,我正在尝试搜索亚马逊。我想选择类别,对于ex.Books,键入一些搜索条件,对于ex.java,然后单击Go按钮。我的问题是点击Go按钮。我有个例外: 线程“main”java.lang.IndexOutOfBoundsException中的异常:索引: 0,大小:0位于java.util.ArrayList.rangeCheck(ArrayList.java:571) get(ArrayList.java:349)位于 在Bot.main(Bot.java:111)中单击SubmitButto

我正在尝试搜索亚马逊。我想选择类别,对于ex.Books,键入一些搜索条件,对于ex.java,然后单击Go按钮。我的问题是点击Go按钮。我有个例外:

线程“main”java.lang.IndexOutOfBoundsException中的异常:索引: 0,大小:0位于java.util.ArrayList.rangeCheck(ArrayList.java:571) get(ArrayList.java:349)位于 在Bot.main(Bot.java:111)中单击SubmitButton(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();
    }
}
/**
*@作者ivan.bisevac
*/
导入java.io.IOException;
导入java.net.MalformedURLException;
导入com.gargoylesoftware.htmlunit.failinghttpstatuscodecoexception;
导入com.gargoylesoftware.htmlunit.WebClient;
导入com.gargoylesoftware.htmlunit.html.HtmlImageInput;
导入com.gargoylesoftware.htmlunit.html.HtmlInput;
导入com.gargoylesoftware.htmlunit.html.HtmlOption;
导入com.gargoylesoftware.htmlunit.html.HtmlPage;
导入com.gargoylesoftware.htmlunit.html.HtmlSelect;
导入com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
公共类Bot{
私有HtmlPage当前页面;
公共HtmlPage getCurrentPage(){
返回当前页面;
}
公共Bot(){
}
/**
*建造商
* 
*@param pageAddress
*要去的地址。
*@抛出异常
*@抛出错误的DurException
*@引发FailingHttpStatusCodeException失败
*/
公共Bot(字符串pageAddress)引发FailingHttpStatusCodeException,
畸形的异常,IOException{
这个();
此.goToAddress(页面地址);
}
/**
* 
*@param pageAddress
*@引发FailingHttpStatusCodeException失败
*@抛出错误的DurException
*如果pageAddress的格式不好(例如,它只是
*www.google.com不带http://)则会引发此异常
*@抛出异常
*/
public void goToAddress(字符串页面地址)
引发FailingHttpStatusCodeException、MalformedUrlexException、,
IOException{
WebClient WebClient=新的WebClient();
currentPage=webClient.getPage(页面地址);
}
/**
*将文本填充到输入字段中
* 
*@param inputId
*标签id
*@param textValue
*要填入输入字段的文本
*/
public void setInputValue(字符串inputId,字符串textValue){
HtmlInput=(HtmlInput)currentPage.getElementById(inputId);
input.setValueAttribute(textValue);
}
/**
* 
*@param buttonId
*按钮id
*@抛出异常
*/
public void clickImageButton(字符串xpathExpr)引发IOException{
HtmlImageInput按钮=(HtmlImageInput)当前页面
.getFirstByXPath(xpathExpr);
currentPage=(HtmlPage)按钮。单击();
}
/**
* 
*@param radioButtonId
*@param radioButtonOption
*@抛出异常
*@抛出中断异常
*/
public void selectRadioButton(字符串radioButtonId,字符串RadioButtonOptions)
抛出IOException、InterruptedException{
最终HtmlInput收音机=(HtmlInput)当前页面
.getElementById(radioButtonId);
收音机。单击();
睡眠(10000);
}
/**
* 
*@param-dropListId
*@param液滴选择
*/
public void selectDropList(字符串dropListId、字符串dropListOption){
HtmlSelect select=(HtmlSelect)currentPage.getElementById(dropListId);
HtmlOption=select.getOptionByValue(dropListOption);
select.setSelectedAttribute(选项,true);
}
公共静态void main(字符串[]args)引发IOException{
机器人=新机器人(“http://www.amazon.com");
bot.selectDropList(“searchDropdownBox”,“search alias=stripbooks”);
setInputValue(“twotabsearchtextbox”、“java”);
bot.clickImageButton(“//div[@id='navGoButton']]/input”);
bot.getCurrentPage().getTitletText();
}
}
显然,clickSumbitButton方法在选择div中的输入元素时存在一些问题。它给出了空数组。有人能帮我解决这个问题吗

编辑:重构方法点击ImageButton后,我在第行出现错误: currentPage=(HtmlPage)按钮。单击(); 下面是堆栈跟踪:

线程“main”java.lang.NullPointerException中的异常位于 单击Bot.main(Bot.java:114)上的ImageButton(Bot.java:81)

你试过了吗

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

我还建议您看看:

我编辑了我的问题。你们能给我一些选择这个图像输入的方法吗?若button.click()触发了一个NullPointerException,那个么button显然是空的。这意味着你没有正确地得到它。然而,从我在amazon看到的代码来看,我提供的解决方案应该是可行的。尝试用System.out.println(currentPage.asXml())调试它;因为你可能错过了其他东西(可能你不在你认为你是的网页中),我不知道怎么做,但现在我没有例外。谢谢