2013-07-30 27 views
0

自動化論壇發帖我需要programattically使用到的HtmlUnit取代從我的程序代碼文本區: http://puu.sh/3PLT0.png上的HtmlUnit vBulletin

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
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();