HtmlUnit - 获取onclick函数的结果

时间:2013-06-29 11:40:25

标签: java javascript htmlunit

我需要获取onlick函数的结果(名为getDetails)并使用其结果。我不介意结果是否是纯文本。

<table id="table-registered" class="table-results">
<thead>
    <tr>
        <th class="col-companyname">Legal/Trading Name</th>
        <th class="col-companytype">Type</th>
        <th class="col-acn">ACN</th>
        <th class="col-abn">ABN</th>
        <th class="col-arbn">ARBN</th>
        <th class="col-state">State</th>
        <th class="col-docimage">Docs</th>
    </tr>
</thead>
<tr id='data_25'>
    <td colspan="7" class="row">
        <table class="table-inner">
            <tr>
                <td class="col-companyname" onclick="getDetails('25', 'ARBN=BN98135544&CompName=A%2EAA+AARON+ACTIVE+PLUMBING+GASFITTING+%26amp%3Bamp%3B+DRAINING+24+HOUR+EME&CompStat=REGD&Type=BUSN&BusIDType=Number&BusID=113963483&=')" style="cursor:pointer;">A.AA AARON ACTIVE PLUMBING GASFITTING &amp; DRAINING 24 HOUR EME</td>
                <td class="col-companytype">Business Name</td>
                <td class="col-acn">&nbsp;</td>
                <td class="col-abn">&nbsp;</td>
                <td class="col-arbn">BN98135544</td>
                <td class="col-state">&nbsp;</td>
                <td class="col-docimage">&nbsp;</td>
            </tr>
            <tr id='row_25' style="display:none;">
                <td colspan="7">
                    <div id=div_25>
                    </div>
                </td>
            </tr>
        </table>
    </td>
</tr>

........

我尝试使用HtmlUnit开发解决方案,但它似乎无法正常工作。

public static void main(String []args){
        final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_17);
        HtmlPage page = null;
        try {
            webClient.setAjaxController(new NicelyResynchronizingAjaxController());
            webClient.waitForBackgroundJavaScript(10000);
            webClient.waitForBackgroundJavaScriptStartingBefore(10000);
            page = webClient.getPage("http://www.abnsearch.com.au/express/results/asic_list.asp?Name=ACTIVE%20PLUMBING%20PTY%20LTD&SearchOptions=1|ASC|0&SearchCount=5");

            final HtmlTable table = page.getHtmlElementById("table-registered");

            for (final HtmlTableRow row : table.getRows()) {
                System.out.println("Found row");


                String onclickAttr = row.getOnClickAttribute();
                ScriptResult result = page.executeJavaScript(onclickAttr);
                System.out.println(result.getJavaScriptResult());

                for (final HtmlTableCell cell : row.getCells()) {
                    System.out.println("   Found cell: " + cell.asText());
                    String onclickAttr2 = cell.getOnClickAttribute();
                    ScriptResult result2 = page.executeJavaScript(onclickAttr2);
                    System.out.println(result2.getJavaScriptResult());

                }
            }
        } catch (FailingHttpStatusCodeException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

我得到的是net.sourceforge.htmlunit.corejs.javascript.Undefined@194f2e8

我也尝试在每一行和每列上调用click(),但它也没有用 例如:

HtmlPage x = (HtmlPage)row.click();
System.out.println("   Object: " + x.getTextContent());

提前致谢

1 个答案:

答案 0 :(得分:0)

您需要获取因javascript执行而返回的页面:

ScriptResult result = page.executeJavaScript(onclickAttr);
Page resultPage = result.getNewPage();