2

基本的なwebClient.getPageメソッドを使用して認証後にページを取得していますが、このWebサイトはある種のcomet / meteorサーバーを使用して、終了しないajaxリクエストを作成するため、getPageがループに入り、次のようになります。

2012年6月22日15:40:15com.gargoylesoftware.htmlunit.IncorrectnessListenerImplnotify警告:廃止されたコンテンツタイプが見つかりました:'application/x-javascript'。

javascriptを一度に無効にすると、ソースページが表示され、ハングしなくなります。

 webClient.setJavaScriptEnabled(false);

しかし、JavaScriptイベントのあるボタンをクリックするなどのHtmlUnit機能を使用することはできません。私はこの問題に直面した最初の人ではないと思いますが、そこにまともな解決策を見つけることができないようです。

私が接続しようとしているページはFacebookです、これは私のコードです:

public static void submittingForm() throws Exception {
      //  final WebClient webClient = new WebClient();
        final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
        webClient.setJavaScriptEnabled(true);
        webClient.setTimeout(60000);
        webClient.setRedirectEnabled(true);
        webClient.setThrowExceptionOnFailingStatusCode(false);
        webClient.setThrowExceptionOnScriptError(false);
        webClient.setCssEnabled(false);
        webClient.setUseInsecureSSL(true);

        // Get the first page
        final HtmlPage page1 = webClient.getPage("http://www.facebook.com");

        // Get the form that we are dealing with and within that form, 
        // find the submit button and the field that we want to change.
        final HtmlForm form = page1.getHtmlElementById("login_form");

        final HtmlTextInput textFieldUsername = form.getInputByName("email");
        final HtmlPasswordInput textFieldPassword = form.getInputByName("pass");
        final HtmlSubmitInput button = form.getInputByValue("Log In");


        // Change the value of the text field
        textFieldUsername.setValueAttribute("emailhere/username");
        textFieldPassword.setValueAttribute("password here");

        // Now submit the form by clicking the button and get back the second page.
        // And get the cookie set up.
        final HtmlPage page2= button.click();

        //Go to the bob marley fan page
        HtmlPage fanPage = webClient.getPage("http://www.facebook.com/BobMarley");
        webClient.setJavaScriptEnabled(true);

        // Get the label that containes the like button from the fan page
        HtmlLabel likeLabel = fanPage.getHtmlElementById("timelineHeadlineLikeButton");


        try{
            // Get the like button
            HtmlSubmitInput likeButton = (HtmlSubmitInput)likeLabel.getLastChild();
            // Press it
            likeButton.click();
        } catch (Exception e){
            e.printStackTrace();
        }

        webClient.closeAllWindows();
    }
4

1 に答える 1

1

このスレッドがまだ開いていて未解決であると仮定します。この問題に遭遇する可能性のある人のために:

HtmlUnit を使用して内部 Web サイト (Web に公開されていない) にログインしようとしました。遭遇したのと同じメッセージを表示してハングしました。

Oct 09, 2013 1:39:59 PM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered: 'application/x-javascript'.

ループに陥りました。これは、オフにできる単なる警告です。スクリプトがハングする理由の 1 つは、JavaScript が読み込まれるのを待っていることが原因である可能性があります。

接続タイムアウトを設定しました。以下を使用して JavaScript タイムアウトを設定してみてください。

webClient.setJavaScriptTimeout(45000);  //Set JavaScript Timeout to 0.75 minute

私も同じことをしましたが、うまくいきました。タイムアウトになり、残りのコード行を実行し続けました。

次の出力が得られました。

INFO: Caught script timeout error
com.gargoylesoftware.htmlunit.javascript.TimeoutError: Javascript execution takes too long (allowed: 45000, already elapsed: 45001)
................

応答しないスクリプトがログイン操作にとって重要でない場合、コードは正常に動作します。

于 2013-10-09T18:52:03.127 に答える