如何在HtmlUnit中通过Xpath获取元素
我正在尝试搜索亚马逊。我想选择类别,例如。书籍,输入一些搜索条件,例如。 java 并单击“Go”按钮。我的问题是单击“Go”按钮。我有例外:
线程“main”中出现异常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();
}
}
显然,在选择 div 内的输入元素时,clickSumbitButton 方法中存在一些问题。它给出空数组。有人会帮我解决这个问题吗?
编辑:重构方法 clickImageButton 后,我在线出现错误: currentPage = (HtmlPage) 按钮.click(); 这是堆栈跟踪:
线程“main”中出现异常 java.lang.NullPointerException Bot.clickImageButton(Bot.java:81) 在 Bot.main(Bot.java:114)
I am trying to search Amazon. I want to choose category, for ex. Books, type some search criteria, for ex. java and to click Go button. My problem is clicking the Go button. I've got 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)
Here is my code:
/**
* @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 there is some problem in method clickSumbitButton, in selecting input element inside div. It gives empty array. Would someone help me to solve this problem?
Edit: After refactoring method clickImageButton, I have error on line:
currentPage = (HtmlPage) button.click();
Here is stack trace:
Exception in thread "main" java.lang.NullPointerException at
Bot.clickImageButton(Bot.java:81) at Bot.main(Bot.java:114)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你尝试过吗?
我还建议您看一下: getFirstByXPath
Have you tried?
I would also recommend you to take a look at: getFirstByXPath