繁体   English   中英

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

[英]Programmatically Click HTML button using java

我有一个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

在您的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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM