просмотр веб-страницы с помощью htmlunit - PullRequest
0 голосов
/ 17 ноября 2018

Вот мой код.Я хочу получить доступ к веб-сайту и сравнить свои данные.Я хочу, чтобы java поместил данные в поля и автоматически щелкнул по значению «вычислить дно» и вернул ответ java.

import com.gargoylesoftware.htmlunit.WebClient;

public class MyWebServiceAccess {

  public static void main(String[] args) throws Exception 
  {

        final WebClient webClient = new WebClient();

        final HtmlPage page = webClient.getPage("https://www.socscistatistics.com/tests/signedranks/Default2.aspx");

        // Inputs
        HtmlTextInput treatment1 = (HtmlTextInput) page.getElementById("ctl00_MainContent_TextBox1");
        HtmlTextInput treatment2 = (HtmlTextInput) page.getElementById("ctl00_MainContent_TextBox2");

        // Significance Level:
        HtmlRadioButtonInput s1= (HtmlRadioButtonInput) page.getElementById("ctl00_MainContent_RadioButtonList1_0");
        HtmlRadioButtonInput s2= (HtmlRadioButtonInput) page.getElementById("ctl00_MainContent_RadioButtonList1_1");


        // 1 or 2-tailed hypothesis?:
        HtmlRadioButtonInput t1= (HtmlRadioButtonInput) page.getElementById("ctl00_MainContent_RadioButtonList2_0");
        HtmlRadioButtonInput t2= (HtmlRadioButtonInput) page.getElementById("ctl00_MainContent_RadioButtonList2_1");

        // Calculate
        HtmlSubmitInput Calculate= (HtmlSubmitInput) page.getElementById("ctl00_MainContent_Button2");

        // Result Span
        HtmlSpan result = (HtmlSpan) page.getElementById("ctl00_MainContent_Label9");

        // Fill in Inputs 
        treatment1.setValueAttribute("");
        treatment2.setValueAttribute("");

        s1.setChecked(true);
        s2.setChecked(false);

        t1.setChecked(true);
        t2.setChecked(false);

        Calculate.click();

        // Printing the Output
        System.out.println(result.asText());

        webClient.closeAllWindows();

   }

}

1 Ответ

0 голосов
/ 17 ноября 2018

При отбраковке веб-страниц вам необходимо иметь общее представление о том, как работают веб-технологии (http / html), а также вам нужны некоторые знания Java / программирования.По крайней мере, очень полезно иметь возможность находить проблемы в ваших программах.

Сначала ваш код создает исключение приведения класса, потому что поля ввода являются текстовыми областями, а не элементами управления вводом текста.Через секунду вы должны нажать правую кнопку (ваш код нажимает кнопку «перезагрузить»).И, наконец, вы получили новую страницу, если кнопка нажата.Ваш результат на новой странице.

Надеюсь, это поможет ....

String url = "https://www.socscistatistics.com/tests/signedranks/Default2.aspx";                                   

try (final WebClient webClient = new WebClient()) {                                       
    HtmlPage page = webClient.getPage(url);                                                                        

    // Inputs                                                                                                      
    HtmlTextArea treatment1 = (HtmlTextArea) page.getElementById("ctl00_MainContent_TextBox1");                    
    HtmlTextArea treatment2 = (HtmlTextArea) page.getElementById("ctl00_MainContent_TextBox2");                    

    // Significance Level:                                                                                         
    HtmlRadioButtonInput s1= (HtmlRadioButtonInput) page.getElementById("ctl00_MainContent_RadioButtonList1_0");   
    HtmlRadioButtonInput s2= (HtmlRadioButtonInput) page.getElementById("ctl00_MainContent_RadioButtonList1_1");   
    s1.setChecked(true);                                                                                           
    s2.setChecked(false);                                                                                          

    // 1 or 2-tailed hypothesis?:                                                                                  
    HtmlRadioButtonInput t1= (HtmlRadioButtonInput) page.getElementById("ctl00_MainContent_RadioButtonList2_0");   
    HtmlRadioButtonInput t2= (HtmlRadioButtonInput) page.getElementById("ctl00_MainContent_RadioButtonList2_1");   
    t1.setChecked(true);                                                                                           
    t2.setChecked(false);                                                                                          

    // Fill in Inputs                                                                                              
    treatment1.type("4\n3\n2\n5\n5\n3");                                                                           
    treatment2.type("1\n2\n3\n0\n0\n2");                                                                           

    // click Calculate creates a new page                                                                          
    HtmlSubmitInput calculate= (HtmlSubmitInput) page.getElementById("ctl00_MainContent_Button1");                 
    page = calculate.click();                                                                                      

    // Result Span                                                                                                 
    HtmlSpan result = (HtmlSpan) page.getElementById("ctl00_MainContent_Label9");                                  

    // Printing the Output                                                                                         
    System.out.println(result.asText());                                                                           
}                                                                                                                  
...