以编程方式单击使用java的HTML按钮

时间:2015-09-24 02:09:45

标签: java html button

我有一个html文件,我想让程序在每次运行程序时单击该按钮。我使用HTMLUnit但仍然有错误。

public class checkBox {
    public static void main(String[] args){
        check();
    }

  public static void check() {
    WebClient wclient = new WebClient();    

    try(final WebClient webClient = new WebClient(BrowserVersion.CHROME)) {

          final HtmlPage Page = wclient.getPage("file://E:\\Please.html");
          final HtmlForm Form = Page.getForms().get(1);
          final HtmlButton submit = Form.getButtonByName("submit");
          final HtmlPage nextPage = submit.click();  
          System.out.println(nextPage);
        } catch(FailingHttpStatusCodeException e) {
            e.printStackTrace();
        } catch(Exception e) {
            e.printStackTrace();
        }
    wclient.close();
  }
}

这是我要点击的按钮。

input type="submit" name="submit" value="Search"

结果显示错误

?? 24, 2015 10:40:10 ?? com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
??: Obsolete content type encountered: 'text/javascript'.
?? 24, 2015 10:40:12 ?? com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
??: Obsolete content type encountered: 'text/javascript'.
?? 24, 2015 10:40:16 ?? com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
??: Obsolete content type encountered: 'text/javascript'.
?? 24, 2015 10:40:16 ?? com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter runtimeError
??: runtimeError: message=[The data necessary to complete this operation is not yet available.] sourceName=[http://www.wipo.int/romarin//shared/scripts/jquery-1.7.js] line=[2] lineSource=[null] lineOffset=[0]
?? 24, 2015 10:40:16 ?? com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument getElementById
??: getElementById(script1443062416699) did a getElementByName for Internet Explorer

1 个答案:

答案 0 :(得分:0)

wclient 声明之前添加此代码,它将隐藏不必要的HtmlUnit日志:

LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");

java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF); 
java.util.logging.Logger.getLogger("org.apache.commons.httpclient").setLevel(Level.OFF);