2

I have an html that looks like this

<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

I want to get the "anchorTwo" and put it inside a HTMLAnchorElement so that I can click it. But when I try to do it with my code, I get a NULL HTMLAnchorElement. Does anyone know a way to work around this? I've been trying for hours, I can't seem to find a way.

This is my code:

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();
thecodeexplorer
  • 363
  • 1
  • 6
  • 18
  • That Html file is a disaster, I doubt it can be "navigated". Fix it and you'll get the content right. – Jimi Jul 22 '18 at 23:35
  • That's the problem. I can't fix it because it has already been created. And I doubt I am allowed to fix it because it is already being used in production. – thecodeexplorer Jul 23 '18 at 00:08

1 Answers1

2

use

HTMLDocument frameDocument = (HTMLDocument)frame.contentWindow.document

instead of

HTMLDocument frameDocument = frame.document;
Prabu
  • 77
  • 1
  • 7