简体   繁体   中英

Tag Iframe doesn't work after click Anchor in HtmlUnit

everyone. I'm learning working with HtmlUnit. I'm with a problem. When I click in a anchor my page has a iFrameTag that doesn't load. The oficial page work normal.

Follow part my code: --Head WebClient client = new WebClient(BrowserVersion.CHROME);

    CookieManager cookieManager = client.getCookieManager();
    cookieManager.setCookiesEnabled(true);
    client.setCookieManager(cookieManager);

    client.getOptions().setJavaScriptEnabled(true);
    client.setAjaxController(new NicelyResynchronizingAjaxController());
    client.getOptions().setThrowExceptionOnScriptError(false);
    client.getOptions().setThrowExceptionOnFailingStatusCode(false);
    client.getOptions().setCssEnabled(false);
    client.getOptions().setUseInsecureSSL(true);

--End Head

    (...)
    HtmlAnchor linkEdital =  (HtmlAnchor) pageEdital.getByXPath("//a[@data-acao='edital']").get(0);
    pageEdital = linkEdital.click();
    client.waitForBackgroundJavaScript(60000);
    System.out.println(pageEdital.asXml());

But, the variable pageEdital doesn't change.

I thanks a lot.

The content of the iframe is not included per default, you have to look inside yourself.

This works here (maybe you can lower the wait times)

public static void main(String[] args) throws Exception {
    try (final WebClient webClient = new WebClient(BrowserVersion.BEST_SUPPORTED)) {
        webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
        webClient.getOptions().setThrowExceptionOnScriptError(false);

        HtmlPage htmlPage = webClient.getPage("https://www.peintegrado.pe.gov.br/portal/Mural.aspx");
        webClient.waitForBackgroundJavaScript(20_000);

        DomNodeList tds = htmlPage.getElementsByTagName("td");
        HtmlTableDataCell td = (HtmlTableDataCell) tds.get(1);
        td.click();
        webClient.waitForBackgroundJavaScript(20_000);

        HtmlAnchor linkEdital = (HtmlAnchor) htmlPage.getByXPath("//a[@data-acao='edital']").get(0);
        linkEdital.click();
        webClient.waitForBackgroundJavaScript(20_000);

        System.out.println("********************");
        System.out.println(htmlPage.asXml());
        System.out.println("********************");

        // dig into the iframe yourself
        HtmlInlineFrame iframe = (HtmlInlineFrame) htmlPage.getElementById("frameResumoEdital");
        HtmlPage iPage = (HtmlPage) iframe.getEnclosedWindow().getEnclosedPage();
        System.out.println("********************");
        System.out.println(iPage.asText());
        System.out.println("********************");
    }
}

For more details look at https://github.com/HtmlUnit/htmlunit/issues/341

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM