0

我需要获取 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());

提前致谢

4

1 回答 1

0

您需要获取作为 javascript 执行结果返回的页面:

ScriptResult result = page.executeJavaScript(onclickAttr);
Page resultPage = result.getNewPage();
于 2013-07-03T02:19:42.180 回答