Automation using mshtml dll c# for webforms getelementbytagname doesnt work

1.1k views Asked by At

I am trying to automate a flow of logging in to a third party website and fetching data using C#, mshtml dll and SHDocVw.dll in webforms.

I am able to login, Fill textboxes, click on button, navigate to new page. I am using getelementbyid method to get the details of the above controls to perform action.

On using getelementbytagname, It's not fetching any data.

Elements, elements1, elements 2 comes as black. No values are populated, even though html has these tags and tag ids.

Here is the code:

SHDocVw.InternetExplorer ie = new
           SHDocVw.InternetExplorerClass();
SHDocVw.WebBrowser wb = (SHDocVw.WebBrowser)ie;
object nullObject = null;
wb.Visible = true;
wb.Navigate("url", ref nullObject, ref nullObject, ref nullObject, ref 
nullObject);           
while (wb.Busy) { Thread.Sleep(100); }           
HTMLDocument document = ((HTMLDocument)wb.Document);
IHTMLElement element = document.getElementById("username");
HTMLInputElementClass email = (HTMLInputElementClass)element;
email.value = "abc";
email = null;
element = document.getElementById("password");
HTMLInputElementClass pass = (HTMLInputElementClass)element;
pass.value = "Admin123";
pass = null;
element = document.getElementById("btnSubmit_6");
HTMLInputElementClass subm = (HTMLInputElementClass)element;
subm.click();
subm = null;
wb.Navigate("url1");
wb.Navigate("url2");
element = document.getElementById("action_46");
HTMLInputElementClass Cont = (HTMLInputElementClass)element;
Cont.click();
Cont = null;
wb.Navigate("url3");
element = document.getElementById("username_53");
HTMLInputElementClass login = (HTMLInputElementClass)element;
login.value = "abc";
login = null;
element = document.getElementById("password_50");
HTMLInputElementClass passwd = (HTMLInputElementClass)element;
passwd.value = "abcd";
passwd = null;
element = document.getElementById("userDomain_2");
HTMLInputElementClass domain = (HTMLInputElementClass)element;
domain.value = "abc";
domain = null;
element = document.getElementById("action_49");
HTMLInputElementClass submt = (HTMLInputElementClass)element;
submt.click();
submt = null;
wb.Navigate("url4");
//Select Node
IHTMLElementCollection elements = document.getElementsByTagName("div");
IHTMLElementCollection elements1 =                     document.getElementsByTagName("//div[@id='postB']");
IHTMLElementCollection elements2 = (IHTMLElementCollection)document.getElementById("postB");

Please find HTML code which I am trying to retrieve. There are many tags with id postA. I need to get all the tags and search for a text in the content of the tag and display. Or is there any way I can search for a particular text in entire html page?

<DIV id="postA">[Aug 01 09h43:20.934] - <a href="javascript://" 
onClick="toggle(this)">Message from 18998652 [161.126.169.35,3878]</a>
<DIV id="postB"><UL>
<PRE format=object>1420:
[LLVAR  n    ..19 019] 002 [7077186160620160159] 
[Fixed  n       6 006] 003 [000000] 
[None   n         012] 004 [000000160000] 
[Fixed  n       6 006] 011 [040174] 
[Fixed  n      12 012] 012 [170801114140] 
[Fixed  n       4 004] 014 [1906] 
[Fixed  n       3 003] 024 [400] 
[Fixed  n       4 004] 025 [4000] 
[Fixed  ans     6 006] 038 [477762] 
[Fixed  n       3 003] 039 [000] 
[Fixed  ans     8 008] 041 [18998652] 
[Fixed  ans    15 015] 042 [189986         ] 
[LLVAR  ans  ..99 040] 043 [Shell\NL Unmanned 80998\Netherlands  \NL] 
[LLLVAR b   ..999   041]    048
[Fixed  a       2 002] 003 [EN] 
[Fixed  n      10 010] 004 [0000000231] 
[Fixed  ans     2 002] 014 [23] 
</PRE format=object><DIV  id="postC"><a href="javascript://" 
onClick="toggle(this)">binary data</a>
<DIV id="postD"><UL><PRE format=object>
0000(0000)  31 34 32 30 70 34 01 80  06 e1 89 01 31 39 37 30   
1420p4......1970
</PRE format=object></UL></DIV></DIV>
</UL><HR></DIV>
</DIV>
<!--EOM-->
<DIV id="postA">[Aug 01 09h43:20.953] - <a href="javascript://" 
onClick="toggle(this)">&lt;0420&gt; Message to Transaction Manager 
[10.104.15.12,4008]</a>
<DIV id="postB"><UL>
<PRE format=object>0420:
[LLVAR  n    ..19 019] 002 [7077186160620160159] 
[Fixed  n       6 006] 003 [000000] 
[None   n         012] 004 [000000160000] 
[Fixed  n      10 010] 007 [0801094320] 
[Fixed  n       6 006] 011 [040174] 
[Fixed  n       6 006] 012 [114140] 
[Fixed  n       4 004] 013 [0801] 
[Fixed  n       4 004] 014 [1906] 
[Fixed  n       3 003] 022 [000] 
[Fixed  n       2 002] 025 [00] 
[Fixed  n       2 002] 026 [12] 
[LLVAR  n    ..11 003] 032 [528] 
[Fixed  ans     6 006] 038 [477762] 
[Fixed  ans     8 008] 041 [18998652] 
[Fixed  ans    15 015] 042 [NL9986000000000] 
[Fixed  ans    40 040] 043 [Shell NL Unmanned 80998Netherlands  NLNL] 
[Fixed  a/n     3 003] 049 [978] 
[LLLVAR n       4 004] 056 [4000] 
[LLLVAR ans ..500 266] 059 [0000000231                         
</PRE format=object><DIV  id="postC"><a href="javascript://" 
onClick="toggle(this)">binary data</a>
1

There are 1 answers

1
sureshk328 On
static void Main(string[] args)
    {

        //Start IE with google url
        Process.Start("iexplore", "www.google.com");

        //Wait for page load. This can be avoided but that will extra code
        Thread.Sleep(5000);

        InternetExplorer IEGoogle=null;
        HTMLDocument htmlGoogle;

        //Getting InternetExplorer from all open windows
        SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows(); shellWindows = new SHDocVw.ShellWindows();
        foreach (InternetExplorer browser in shellWindows )
        {
            if(browser.LocationURL.ToLower().Contains("www.google.com"))
            {
                IEGoogle = browser;
                break;
            }
        }

        if(IEGoogle!=null)
        {
            //Get HTML content in HTMLDocument from InternetExplorer object
            htmlGoogle = IEGoogle.Document;

            //Open google.com and Using Developer tools(Press F12) and see the details of searchbox on google.com. I have found that name property of searchbox is unique.
            HTMLInputElement searchbox = htmlGoogle.getElementsByName("q").item() as HTMLInputElement;


            if (searchbox != null)
            {
                searchbox.value = "First Way Oxford Library";
            }

            Console.ReadLine();
            //Alternate way
            var inputElements = htmlGoogle.getElementsByTagName("input");
            if(inputElements!=null)
            {
                foreach(HTMLInputElement googleSearch in inputElements)
                {
                    if (googleSearch != null)
                    {
                        if (googleSearch.name != null)
                        {
                            if (googleSearch.name.Equals("q"))
                            {
                                googleSearch.value = " Second Way Oxford Library";
                            }
                        }
                    }

                }
            }

        }



    }