簡體   English   中英

使用mshtml dll c#for webforms進行自動化getelementbytagname不起作用

[英]Automation using mshtml dll c# for webforms getelementbytagname doesnt work

我正在嘗試自動化登錄到第三方網站並在Webforms中使用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>
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";
                            }
                        }
                    }

                }
            }

        }



    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM