dongzhi4690 2013-07-30 23:02
浏览 103

HtmlUnit在vbulletin上自动发布论坛

I need to replace this text area with code from my program programattically using 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();
}

}

I don't know how to get the text area, yet alone change the value using htmlunit! Please tell me what's wrong with my code!

  • 写回答

1条回答 默认 最新

  • doupeng3501 2013-07-31 06:05
    关注
    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();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog