htmlunit如何不通过id和name获取button,并提交表单。跪求高手您是如何解决的??

如题所述

登录按钮:

<input name="" type="image" src="../images/button_login.gif" class="input_img">

实现方法:

我用的class,当然也可以换成type或src,属性都可以作为判断条件

HtmlImageInput btnSubmit=null;

DomNodeList<DomElement> domElements=htmlpage.getElementsByTagName("input");

for(DomElement temp:domElements){

if(temp.getAttribute("class").equals("input_img")){

btnSubmit= (HtmlImageInput) temp;

}

}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-04-20

我是htmlunit 2.14,还是使用了name和id。如果不使用name和id,可以尝试一下xpath。

下面是使用getElementByName的代码(来自于statckoverflow,亲自测试过可以使用)。

     import com.gargoylesoftware.htmlunit.WebClient;
     import java.io.*;
     import com.gargoylesoftware.htmlunit.html.HtmlPage;    
     import com.gargoylesoftware.htmlunit.html.HtmlInput;
     import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;


     import java.net.*;

      public class GoogleSearch {

     public static void main(String[] args)throws IOException, MalformedURLException
     {
       final WebClient webClient = new WebClient();

       HtmlPage page1 = webClient.getPage("http://www.google.com");
       HtmlInput input1 = page1.getElementByName("q");
       input1.setValueAttribute("yarn");

       HtmlSubmitInput submit1 = page1.getElementByName("btnG");

       page1=submit1.click();

       System.out.println(page1.asXml()); 

       webClient.closeAllWindows();
     }
   }





来自:求助得到的回答
第1个回答  2014-04-20
jquery 使用很多方法 如 class=