我需要使用htmlunit以编程方式用我的程序替换此文本区域: http://puu.sh/3PLT0.png
package com.cartoonamon;
import java.io.IOException;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextArea;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
public class Application {
public static void main(String[] args) {
new Application();
}
public Application() {
System.setProperty("socksProxyHost", "localhost");
System.setProperty("socksProxyPort", "9050");
final WebClient webClient = new WebClient();
webClient.getOptions().setJavaScriptEnabled(false);
webClient.getOptions().setCssEnabled(false);
HtmlPage page;
try {
page = webClient.getPage("http://cartoonamon.com/forum.php");
HtmlForm login = null;
for (HtmlForm f : page.getForms()) {
if (f.getId().equalsIgnoreCase("navbar_loginform")) {
login = f;
}
}
HtmlTextInput username = login.getElementById("navbar_username");
HtmlPasswordInput password = login.getElementById("navbar_password");
HtmlSubmitInput submit = login.getInputByValue("Log in");
username.setValueAttribute("no looking");
password.setValueAttribute("no looking");
HtmlPage page2 = submit.click();
HtmlPage postThread = webClient.getPage("http://cartoonamon.com/newthread.php?do=newthread&f=26");
postThread.getFormByName("vbform");
HtmlTextInput subject = (HtmlTextInput) postThread.getElementById("subject");
subject.setValueAttribute("test");
//HtmlTextArea contentBox = (HtmlTextArea)
System.out.println(postThread.getBody().asText());
//contentBox.type("test12345678910");
} catch (FailingHttpStatusCodeException | IOException e) {
e.printStackTrace();
}
webClient.closeAllWindows();
}
}
我不知道如何获取文本区域,但是单独使用htmlunit更改值!请告诉我我的代码有什么问题!
答案 0 :(得分:0)
some example
1.choose drop down
// select option ALL
HtmlSelect select = (HtmlSelect) currentPage.getElementByName("index");
HtmlOption option = select.getOptionByValue("All");
select.setSelectedAttribute(option, true);
2.
// Enter text
HtmlInput queryInput = currentPage.getElementByName("query");
queryInput.setValueAttribute(searchValue);
3.
// click button
HtmlSubmitInput submitBtn = currentPage.getFirstByXPath("//input[@value='Search']");
currentPage = submitBtn.click();