在htmlunit中更改所选选项后,页面不会更新

时间:2015-09-15 09:46:19

标签: java htmlunit

我是htmlunit的新手。它很顺利,直到我想在我们的大学网站上改变课程类型。问题是,当我更改所选选项时,页面内容不会更新,结果也相同。我怎么能解决这个问题?

public static void main(String [] args ) throws FailingHttpStatusCodeException, MalformedURLException, IOException, InterruptedException 
{
    WebClient webClient = new WebClient(BrowserVersion.CHROME);
    webClient.getOptions().setRedirectEnabled(true);
    webClient.setAjaxController(new NicelyResynchronizingAjaxController());
    webClient.getCookieManager().setCookiesEnabled(true);

    java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF); 
    System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");

    final HtmlPage page = webClient.getPage("http://amozesh.araku.ac.ir");
    HtmlTextInput name = (HtmlTextInput) page.getElementById("UserCode");
    HtmlPasswordInput pass = (HtmlPasswordInput)page.getElementByName("KeyCode");
    HtmlElement submitButton = (HtmlElement) page.getElementById("input");
    name.setText(user);
    pass.setText(passs);

    System.out.println(page.getTitleText());
    HtmlPage pagePassVarified = submitButton.click();
    System.out.println(pagePassVarified.getTitleText());
    //successfully login

    final HtmlPage page2 = webClient.getPage("http://amozesh.araku.ac.ir/LessonReport.asp");

    HtmlSelect select = (HtmlSelect) page2.getElementByName("LessonType");
    System.out.println(page2.asText()); // i get content before changing selected option
    System.out.println(select.getDefaultValue());

    // changing selected option 
    HtmlOption option = select.getOptionByValue("1");
    select.setSelectedAttribute(option, true);

    //i get below section from other's post that have problem like me but      doesn't work for me
    webClient.setAjaxController(new NicelyResynchronizingAjaxController()); 
    webClient.waitForBackgroundJavaScript(10000);
    Thread.sleep(5000);
    synchronized (page2) 
    {
        System.out.println("wait");
        page2.wait(500);
    }


    System.out.println(page2.asText()); //i get content after changing selected option but it still same ):
    System.out.println(select.getDefaultValue());// selected value is changed!



}

0 个答案:

没有答案