IHTMLDocument2类的使用

时间:2024-01-14 15:12:14

class Program
    {
        static void Main(string[] args)
        {
            SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows();
            string filename;
            foreach (SHDocVw.InternetExplorer ie in shellWindows)
            {
                filename = "iexplore";// Path.GetFileNameWithoutExtension(ie.FullName).ToLower();
                if (filename.Equals("iexplore"))
                {
                    Console.WriteLine("Web Site   : {0}", ie.LocationURL);
                    mshtml.IHTMLDocument2 htmlDoc = ie.Document as mshtml.IHTMLDocument2;
                    Console.WriteLine("   Document Snippet: {0}", ((htmlDoc != null) ? htmlDoc.body.outerHTML.Substring(0, 40) : "***Failed***"));
                    if (ie.LocationURL == "http://s.taobao.com/search?q=%D2%C2%B7%FE&initiative_id=tbindexz_20130105&commend=all&source=suggest&ssid=s5-e-p1&suggest=0_3&newpre=null&bcoffset=1&s=40#J_FilterTabBar")
                    {
                        SearchTextInGoogle(htmlDoc, "衣服");
                    }
                    Console.WriteLine("{0}{0}", Environment.NewLine);
                }
            }
            Console.WriteLine("完成");
            Console.ReadLine();
        }
        public static void SearchTextInGoogle(mshtml.IHTMLDocument2 ieDoc, string searchText)
        {
            mshtml.HTMLInputElement input;
           
            //set the text to be searched   
            //foreach (mshtml.IHTMLElement ieElement in ieDoc.all)
            //{
            //    //if its tag is input and name is q(question)   
            //    if (ieElement.tagName.ToUpper() == "INPUT")
            //    {
            //        input = ((mshtml.HTMLInputElement)ieElement);
            //        if (input.name == "wd")
            //        {
            //            input.value = searchText;
            //            //break;
            //        }
            //    }
            //}

//click the submit button to search   
            foreach (mshtml.IHTMLElement ieElement in ieDoc.all)
            {
                //if its tag is input   
                if (ieElement.tagName.ToUpper() == "A")
                {
                   
                    if (ieElement.className == "page-next")
                    {
                        ieElement.click();
                       
                    }
                }
              
            }
        }
    }