HtmlUnit按钮单击
我正在尝试在www.meetme.com上发送消息,但不知道该怎么做。我可以在评论区域中键入消息,但是单击“发送”按钮不会执行任何操作。我究竟做错了什么?当我登录并按登录按钮时,页面确实发生了变化,一切都很好。有人有任何想法或线索吗?
    HtmlPage htmlPage = null;    HtmlElement htmlElement;
    WebClient webClient = null;
    HtmlButton htmlButton;
    HtmlForm htmlForm;
    try{
        // Create and initialize WebClient object
        webClient = new WebClient(BrowserVersion.FIREFOX_17 );
        webClient.setCssEnabled(false);
        webClient.setJavaScriptEnabled(false);
        webClient.setThrowExceptionOnFailingStatusCode(false);
        webClient.setThrowExceptionOnScriptError(false);
        webClient.getOptions().setThrowExceptionOnScriptError(false);
        webClient.getOptions().setUseInsecureSSL(true);
        webClient.getCookieManager().setCookiesEnabled(true);
        /*webClient.setRefreshHandler(new RefreshHandler() {
            public void handleRefresh(Page page, URL url, int arg) throws IOException {
                 System.out.println("handleRefresh");
            }
        });*/
         htmlPage = webClient.getPage("http://www.meetme.com");
         htmlForm = htmlPage.getFirstByXPath("//form[@action='https://ssl.meetme.com/login']");
         htmlForm.getInputByName("username").setValueAttribute("blah@gmail.com");
         htmlForm.getInputByName("password").setValueAttribute("blah");
         //Signing in
         htmlButton = htmlForm.getElementById("login_form_submit");
         htmlPage = (HtmlPage) htmlButton.click();
         htmlPage = webClient.getPage("http://www.meetme.com/member/1234567890");
         System.out.println("BEFORE CLICK");
         System.out.println(htmlPage.asText());
         //type message in text area
         HtmlTextArea commentArea = (HtmlTextArea)htmlPage.getFirstByXPath("//textarea[@id='profileQMBody']");
         commentArea.setText("Testing");
         htmlButton = (HtmlButton) htmlPage.getHtmlElementById("profileQMSend");
         htmlPage = (HtmlPage)htmlButton.click();
         webClient.waitForBackgroundJavaScript(7000);
         //The print is exactly the same as the BEFORE CLICK print
         System.out.println("AFTER CLICK");
         System.out.println(htmlPage.asText());
      }catch(ElementNotFoundException e){
        e.printStackTrace();
      }catch(Exception e){
        e.printStackTrace();
      }
回答:
如果不了解正在访问的网页,就无法在禁用JavaScript的情况下执行AJAX请求。如果更改未成功,则必须继续进行调试,但请确保已启用JavaScript。
此外,请确保您使用的是HtmlUnit 1.12,并更新代码中所有不赞成使用的方法。
顺便说一句,我也建议关闭可能的JavaScript警告。检查此答案以了解如何进行。
要从最新版本的HtmlUnit中删除所有输出,您只需要在静态块或主类中添加以下行即可:
java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF); System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");
不需要覆盖其他任何状态的方法。
以上是 HtmlUnit按钮单击 的全部内容, 来源链接: utcz.com/qa/410358.html

