public static void SelectComboboxValue(string inputId, string value)
        {
            HTMLSelectElementClass input   = (HTMLSelectElementClass)GetElementById(inputId);
            HTMLElementCollection  options = (HTMLElementCollection)input.options;
            HTMLOptionElement      el      = (HTMLOptionElement)options.namedItem(value);

            if (el != null)
            {
                input.selectedIndex = el.index;
            }
        }
Exemple #2
0
        private void button1_Click(object sender, EventArgs e)
        {
            ////// Create a new instance of the Firefox driver.

            ////// Notice that the remainder of the code relies on the interface,
            ////// not the implementation.

            ////// Further note that other drivers (InternetExplorerDriver,
            ////// ChromeDriver, etc.) will require further configuration
            ////// before this example will work. See the wiki pages for the
            ////// individual drivers at http://code.google.com/p/selenium/wiki
            ////// for further information.
            ////IWebDriver driver = new FirefoxDriver();

            //////Notice navigation is slightly different than the Java version
            //////This is because 'get' is a keyword in C#
            ////driver.Navigate().GoToUrl("http://www.google.com/");

            ////// Find the text input element by its name
            ////IWebElement query = driver.FindElement(By.Name("q"));

            ////// Enter something to search for
            ////query.SendKeys("Cheese");

            ////// Now submit the form. WebDriver will find the form for us from the element
            ////query.Submit();

            ////// Google's search is rendered dynamically with JavaScript.
            ////// Wait for the page to load, timeout after 10 seconds
            ////WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
            ////wait.Until((d) => { return d.Title.ToLower().StartsWith("cheese"); });

            ////// Should see: "Cheese - Google Search"
            ////System.Console.WriteLine("Page title is: " + driver.Title);

            //////Close the browser
            ////driver.Quit();
            //string IELocation = @"%ProgramFiles%\Internet Explorer\iexplore.exe";
            //IELocation = System.Environment.ExpandEnvironmentVariables(IELocation);

            //Console.WriteLine("Launching IE ");
            //Process p = Process.Start(IELocation, @"http://www.baidu.com");
            //Thread.Sleep(3000);

            //Console.WriteLine("Attaching to IE ... ");
            //InternetExplorer ie = null;

            //string ieWindowName = "空白页";
            //if (p != null)
            //{
            //    //Console.WriteLine("Process handle is: " + p.MainWindowHandle.ToString());
            //    SHDocVw.ShellWindows allBrowsers = new ShellWindowsClass();
            //    Console.WriteLine("Number of active IEs :" + allBrowsers.Count.ToString());
            //    if (allBrowsers.Count != 0)
            //    {
            //        SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass();
            //        foreach (SHDocVw.InternetExplorer ieVW in shellWindows)
            //        {
            //            // 判斷視窗是否為 iexplore
            //            if (Path.GetFileNameWithoutExtension(ieVW.FullName).ToLower().Equals("iexplore"))
            //            {
            //                ie = ieVW;
            //                break;
            //            }
            //        }


            //        //for (int i = 0; i < allBrowsers.Count; i++)
            //        //{
            //        //    //IWebBrowser2 iwb2 = allBrowsers.Item(i) as IWebBrowser2;

            //        //    InternetExplorer eitem = (InternetExplorer)allBrowsers.Item(i);
            //        //    int l = eitem.HWND;
            //        //    int j = 0;
            //        //    j++;
            //        //    if (eitem.HWND == (int)p.MainWindowHandle.ToInt32())
            //        //    {
            //        //        ie = eitem;
            //        //        break;
            //        //    }
            //        //}
            //    }
            //    else
            //        throw new Exception("Faul to find IE");
            //}
            //else
            //    throw new Exception("Fail to launch IE");

            ////var ie = new SHDocVw.InternetExplorer();
            ////ie.Visible = true;
            ////object o = null;
            ////Console.WriteLine("打开百度首页...");
            ////ie.Navigate(@"http://www.baidu.com", ref o, ref o, ref o, ref o);
            //Thread.Sleep(1000);

            //HTMLDocument doc = (HTMLDocument)ie.Document;

            //Console.WriteLine("网站标题:" + doc.title);

            //Console.WriteLine("输入搜索关键字:飞苔博客");
            //var keyEle = doc.getElementById("kw");
            //keyEle.setAttribute("value", "飞苔博客", 0);

            //Console.WriteLine("点击[百度一下]按钮,进行搜索");
            //var btnSubmit = doc.getElementById("su");
            //btnSubmit.click();
            //Thread.Sleep(500);

            //Console.WriteLine("网站标题:" + doc.title);
            ////var relatedSearchResultEle = doc.getElementsByTagName("SPAN").Cast<ihtmlelement>().Where(ele => ele.className == "nums").FirstOrDefault();
            //Console.WriteLine("百度相关搜索结果:");
            //p.Close();
            ////Console.WriteLine(relatedSearchResultEle.innerHTML);
            object o = null;

            SHDocVw.InternetExplorer ie = new SHDocVw.InternetExplorerClass();

            SHDocVw.WebBrowser wb = (SHDocVw.WebBrowser)ie;
            wb.Visible    = true;
            wb.FullScreen = true;
            //Do anything else with the window here that you wish
            wb.Navigate("http://www.taobao.com/", ref o, ref o, ref o, ref o);
            while (wb.Busy)
            {
                Thread.Sleep(100);
            }
            HTMLDocument          document = ((HTMLDocument)wb.Document);
            IHTMLElement          element  = document.getElementById("q");
            HTMLInputElementClass email    = (HTMLInputElementClass)element;

            email.value = "包装盒子";

            var elements = document.getElementsByTagName("button");

            foreach (HTMLButtonElementClass element1 in elements)
            {
                // If there's more than one button, you can check the
                //element.InnerHTML to see if it's the one you want
                if (element1.innerHTML.Contains("搜 索"))
                {
                    element1.click();
                }
            }
            //for (int i = 0; i < count; i++)
            //{

            //    //去抓標籤名字有a的,像是連結<a href=xx>這種

            //    //奇怪的是這個方法如果要抓一些標籤都抓不到,像是<td><tr>那些=.=

            //    //所以那些我是用另外的方法抓的,等下會講
            //    if (document.all[i].TagName.ToLower().Equals("button"))
            //    {

            //        //GetAttribute就是去取得標籤的屬性的內容,例:

            //        //<a href ="我是屬性的內容" target=_blank>,不過有些屬性取不到,像是class

            //        if (doc.All[i].GetAttribute("type").Contains("submit"))
            //        {                             //InnerText就是取得標籤中間的內容,<標籤>內容</標籤>
            //            //richTextContent1.AppendText(doc.All[i].InnerText);
            //            //richTextContent1.AppendText(doc.All[i].GetAttribute("href"));
            //            //richTextContent1.AppendText(Environment.NewLine);

            //            ////另外這個函式可以去更改HTML裡面屬性的內容
            //            //XXX.SetAttribute("value", "Hello");
            //            int j = i;
            //            j++;
            //        }
            //    }
            //}
            IHTMLElement           search    = document.getElementById(@"tag:BUTTON&value:搜 索");
            HTMLButtonElementClass searchEle = (HTMLButtonElementClass)search;

            searchEle.click();
            //email = null;
            //element = document.getElementById("Passwd");
            //HTMLInputElementClass pass = (HTMLInputElementClass)element;
            //pass.value = "pass";
            //pass = null;
            //element = document.getElementById("signIn");
            //HTMLInputElementClass subm = (HTMLInputElementClass)element;
            //subm.click();
            //subm = null;
            //while (wb.Busy) { Thread.Sleep(100); }
            //wb.Navigate("https://adwords.google.co.uk/o/Targeting/Explorer?", ref o, ref o, ref o, ref o);
            //while (wb.Busy) { Thread.Sleep(100); }
            //string connString = "SERVER=localhost;" +
            //        "DATABASE=test;" +
            //        "UID=root;";

            ////create your mySQL connection
            //MySqlConnection cnMySQL = new MySqlConnection(connString);
            ////create your mySql command object
            //MySqlCommand cmdMySQL = cnMySQL.CreateCommand();
            ////create your mySQL reeader object
            //MySqlDataReader reader;
            ////open the mySQL connection
            //cnMySQL.Open();
            //int j = 1;
            //cmdMySQL.CommandText = "SELECT * FROM `emails1` WHERE `send`>'" + j + "' order by `send` asc limit 0,1";
            //reader = cmdMySQL.ExecuteReader();
            //reader.Read();
            //j = (int)reader.GetValue(1);
            HTMLElementCollection col = default(HTMLElementCollection);

            document = ((HTMLDocument)wb.Document);
            col      = (HTMLElementCollection)document.getElementsByTagName("textarea");
            //the error happens here in this typecast


            //Console.WriteLine(j);
            wb.Quit();
        }