2

我有一个html看起来像这样的

<html>
    <head></head>
    <frameset id="workFrame" rows="45,*" border="0" frameSpacing="0">
        <frame name="menuFrame" id="menuFrame" src="Test.aspx" frameBorder="0" noResize="noresize" scrolling="no">
            <html>
                <head></head>
                <body>
                    <form id="form1" action="./Test.aspx" method="post">
                        <div class="firstDiv"></div>
                        <div class="firstDiv"></div>
                        <div class="secondDiv">
                            <div class="secondDivHeader">
                                <table>
                                    <tbody>
                                        <tr>
                                            <td></td>
                                            <td class ="secondTD">
                                                <table>
                                                    <tbody>
                                                        <tr>
                                                            <td>
                                                                <a id ="anchorOne"></a>
                                                                <a id ="anchorTwo" href="Click.html"></a>
                                                                <a id ="anchorThree"></a>
                                                            </td>
                                                        </tr>
                                                    </tbody>
                                                </table>
                                            </td>
                                            <td></td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                        </div>  
                </body>
            </html>
    </frameset>
</html

我想获取“anchorTwo”并将其放在 HTMLAnchorElement 中,以便我可以单击它。但是当我尝试用我的代码来做这件事时,我得到了一个 NULL HTMLAnchorElement。有谁知道解决这个问题的方法?我已经尝试了几个小时,我似乎找不到办法。

这是我的代码:

InternetExplorer ieObject = new InternetExplorer();
HTMLDocument htmlDocObject = null;
ieObject.Visible = true;
ieObject.Navigate("http://samplewebsite.com");

while (ieObject.ReadyState != tagREADYSTATE.READYSTATE_COMPLETE || ieObject.Busy)
{
    Console.WriteLine("Sleeping for 2 seconds...");
    Thread.Sleep(2000);
}

Console.WriteLine($"Website loaded");
htmlDocObject = ieObject.Document;

HTMLFrameElement frame = (HTMLFrameElement)htmlDocObject.getElementById("menuFrame");
Console.WriteLine(frame.name);
Console.WriteLine(frame.src);

HTMLDocument frameDocument = frame.document;

HTMLAnchorElement anchor = (HTMLAnchorElement)frameDocument.getElementById("anchorTwo"); \\null pointer here

anchor.click();

Console.ReadLine();
4

1 回答 1

2

利用

HTMLDocument frameDocument = (HTMLDocument)frame.contentWindow.document

代替

HTMLDocument frameDocument = frame.document;
于 2018-11-23T07:27:37.587 回答