2017-05-10 2 views
0

を変更しません。HtmlUnitはTextAreaのテキストの私は、このウェブページに接続するためのコードを書かれている

2)コードをコンパイルするボタンが見つかりました。

3)コードの結果を返すtext-areaが見つかりました。

私がtextarea.setText( "something")を呼び出すと、実際にはWebページのコードが変更されないという問題があります。コンパイルボタンをクリックすると、そのページ内のデフォルトコードがコンパイルされ、そのデフォルトコードの出力が返されます。

私はtextareaに焦点を当てようとしましたが、下のものすべてを見ることができます。 私は呼ばれました。

1)textArea.focus();

2)textArea.click();

3)私はtextArea.setAttribute( "name"、 "code")を使って試しました。

私はインターネットを検索し、この問題に近いさまざまなスタックオーバーフローに関する質問を見つけましたが、どちらも問題を解決しておらず、textArea.setText()と言うと誰にとってもうまくいくようです。

私はtextArea.setTextを(「...」)を呼び出し、その後、私が言うならば、私はあなたと共有すべきもう一つの興味深い事実は、

です。

HtmlTextArea textArea1 = form.getTextAreaByName( "code");

私がtextArea1.getText()を呼び出すと、このテキストの値は "..."になります。これは、実際にテキスト領域の値を変更することができたことを意味するはずですが、コンパイルすると、設定したテキストではなくテキスト領域のデフォルトテキストがコンパイルされます。

これに関する助力?

P.S:コンパイルの結果をwhileループに入れるのは、ネットワーク接続の問題です。このコードを実行しようとすると、最初の試行では機能しない可能性があります。また、コンソールに印刷するためにブロックした何千もの警告を与えるので、実行時間は約15秒です。 P.S2:私もこのページを見て、どれもうまくいきませんでした。

http://www.programcreek.com/java-api-examples/index.php?api=com.gargoylesoftware.htmlunit.html.HtmlTextArea

public class Test { 
    public static void main(String[] args) { 
     try { 
      // Prevents the program to print thousands of warning codes. 
      java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(java.util.logging.Level.OFF); 
      java.util.logging.Logger.getLogger("org.apache.http").setLevel(java.util.logging.Level.OFF); 

      // Initializes the web client and yet again stops some warning codes. 
      WebClient webClient = new WebClient(BrowserVersion.CHROME); 
      webClient.getOptions().setThrowExceptionOnFailingStatusCode(false); 
      webClient.getOptions().setThrowExceptionOnScriptError(false); 
      webClient.getOptions().setJavaScriptEnabled(true); 
      webClient.getOptions().setCssEnabled(true); 


      // Gets the html page, which is the online compiler I'm using. 
      HtmlPage page = webClient.getPage("https://www.compilejava.net/"); 


      // Succesfully finds the form which has the required buttons etc. 
      List<HtmlForm> forms = page.getForms(); 
      HtmlForm form = forms.get(0); 


      // Finds the textarea which will hold the code. 
      HtmlTextArea textArea = form.getTextAreaByName("code"); 

      // Finds the textarea which will hold the result of the compilation. 
      HtmlTextArea resultArea = page.getHtmlElementById("execsout"); 

      // Finds the compile button. 
      HtmlButtonInput button = form.getInputByName("compile"); 

      textArea.click(); 
      textArea.focus(); 
      // Simple code to run. 
      textArea.setDefaultValue("public class HelloWorld\n" + 
        "{\n" + 
        " // arguments are passed using the text field below this editor\n" + 
        " public static void main(String[] args)\n" + 
        " {\n" + 
        " System.out.print(\"Hello\");\n" + 
        " }\n" + 
        "}"); 

      System.out.println(textArea.getText()); 

      // Compiles. 
      button.click(); 

      // Result of the compilation. 
      String str = resultArea.getText(); 
      while (resultArea.getText() == null || resultArea.getText().substring(0, 3).equals("exe")) { 
       System.out.print(resultArea.getText()); 
      } 
      System.out.println(); 
      System.out.println(resultArea.getText()); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

答えて

0

少しの忍耐は、ここでは動作しません

 // Result of the compilation. 
     while (resultArea.getText() == null || resultArea.getText().startsWith("exe")) { 
      System.out.println(resultArea.getText()); 
      Thread.sleep(500); 
     } 
     System.out.println(); 
     System.out.println(resultArea.getText()); 
+0

に役立ちます。問題は、TextAreaのテキストを変更できないということです。それは印刷されます。 "なぜ、こんにちは!これは何か?"これはページのデフォルト設定です。私は、おそらくtextArea.setText()メソッドで設定しているコードの結果を出力しようとしています。 – Random

関連する問題