使用mshtml dll c#进行webforms getelementbytagname的自动化不起作用

时间:2017-10-12 05:34:07

标签: c# html asp.net mshtml shdocvw.internetexplorer

我正在尝试自动登录到第三方网站并使用网络表单中的C#,mshtml dll和SHDocVw.dll来获取数据。

我可以登录,填写文本框,单击按钮,导航到新页面。我正在使用getelementbyid方法获取上述控件的详细信息以执行操作。

在使用getelementbytagname时,它没有提取任何数据。

元素,元素1,元素2为黑色。即使html具有这些标记和标记ID,也不会填充任何值。

以下是代码:

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");
  

请找到我想要检索的HTML代码。 id postA有很多标签。我需要获取所有标签并在标签和显示内容中搜索文本。或者有什么方法可以在整个html页面中搜索特定文本吗?

<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 个答案:

答案 0 :(得分:0)

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";
                            }
                        }
                    }

                }
            }

        }



    }