我的页面里包含一个table如下:
<table border="1" name="tbTest">
<tr>
<td><input type="Checkbox" name="chbTest" value="abc"/></td>
<td>test1</td>
<td>test2</td>
</tr>
</table>
我用ActiveX去解析它,但是为什么按table去解析就解析不到inputelement呢??而直接对所有的element解析就可解析到。
我按table方式去解析的代码如下:
var
    browser: IWebBrowser2;
    document: IHTMLDocument2;
    element: IHTMLElement;
    elements: IHTMLElementCollection;
    spDisp : IDispatch;
    i,j,h: Integer;
    htmlTable: IHTMLTable;
    htmlTableRow: IHTMLTableRow;
    htmlInput: IHTMLInputElement;
begin
    with ActiveFormControl.ClientSite as IServiceProvider do
        oleCheck(queryService(IWebbrowserApp,IWebbrowser2,Browser));
    document := browser.Document as IHTMLDocument2;
    elements := document.all;
    for i := 0 to elements.length - 1 do
    begin
        spDisp := elements.item(i,varEmpty);
        if Succeeded(spDisp.QueryInterface(IHTMLTable,htmlTable)) then
        begin
            for j := 0 to htmlTable .rows.length - 1 do
            begin
                spDisp := htmlTable .rows.item(j,varEmpty);
                if Succeeded(spDisp.QueryInterface(IHTMLTableRow,htmlTableRow)) then
                begin
                    for h := 0 to htmlTableRow.cells.length - 1 do
                    begin
                        spDisp := htmlTableRow.cells.item(h,varEmpty);
                        if Succeeded(spDisp.QueryInterface(IHTMLInputElement,htmlInput)) then
                            if htmlInput.checked then
                                showMessage('checked');
                        if Succeeded(spDisp.QueryInterface(IHTMLElement,element)) then
                            memo1.Lines.Add(element.outerText);
                    end;
                end;
            end;
            break;
        end;
    end;
end;
按所有elements方式去解析如下:var
    browser: IWebBrowser2;
    document: IHTMLDocument2;
    element: IHTMLElement;
    elements: IHTMLElementCollection;
    spDisp : IDispatch;
    i: Integer;
    htmlInput: IHTMLInputElement;
    dispHtmlInput: DispHTMLInputElement;
begin
    with ActiveFormControl.ClientSite as IServiceProvider do
        oleCheck(queryService(IWebbrowserApp,IWebbrowser2,Browser));
    document := browser.Document as IHTMLDocument2;
    elements := document.all;
    for i := 0 to elements.length - 1 do
    begin
        spDisp := elements.item(i,varEmpty);
        if Succeeded(spDisp.QueryInterface(IHTMLInputElement,htmlInput)) then
        begin
            dispHtmlInput := htmlInput as DispHTMLInputElement;
            memo1.Lines.Add('name:' + dispHtmlInput.name + ' value:' + dispHtmlInput.value);
            if htmlInput.checked then
                showMessage('Checked');
        end;
end;
请大家帮忙看下,谢谢。

解决方案 »

  1.   

    楼主可以这样写:
    -------------------------------------------------var
      aDoc: IHTMLDocument2;
      tableIntf: IHTMLElement;
      input: IHTMLInputElement;  all:IHTMLElementCollection;
    begin
    //
      aDoc:= WebBrowser1.Document as IHTMLDocument2;  all := aDoc.all.tags('TABLE') as IHTMLElementCollection;;
      tableIntf :=  all.item(0,0) as IHTMLElement;
      
      all := (tableIntf.all as IHTMLElementCollection)
        .tags('input') as IHTMLElementCollection;
      input :=  all.item(0,0) as IHTMLInputElement;  input.checked := true;
      

  2.   

    楼主的问题只需要仔细跟踪调试一下不难发现的..比如在两重循环里面插入下面语句..
     msg := format('j=%d h=%d  %s',[j,h,(spDisp as IHTMLElement).tagname ]);
     ShowMessage(msg);运行就会发现,这一层取到的节点仍是<TD>这一层的..
    而INPUT节点在TD的下一层.
    不喜欢象楼主那样循环遍历,不过当然也是可行的..
    可在两重循环里面下面这样筛得想要的INPUT节点.if assigned((spDisp as IHTMLDOMNode).firstChild) then//有子节点
    begin
      if  Supports((spDisp as IHTMLDOMNode).firstChild,IHTMLInputElement) then
      begin
        htmlinput := (spDisp as IHTMLDOMNode).firstChild as IHTMLInputElement;
        if htmlInput.checked then
           showMessage('checked')
        else
           showMessage('unchecked');
      end;
    end;