简体   繁体   中英

C# and interet explorer automation

Hi i am working on an inte.net explorer automation and this is new territory for me, i am getting an error with a typecast that hasnt succeeded can you see what i am doing wrong?

Unable to cast COM object of type 'System.__ComObject' to interface type 'mshtml.HTMLElementCollection'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{3050F56B-98B5-11CF-BB82-00AA00BDCE0B}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

the code is:

 namespace IEAutomation {
        /// <summary>
        /// Summary description for IEDriverTest.
        /// </summary>
        /// 
        using mshtml;
        using System.Threading;
        using System;
        using SHDocVw;

        using System.Collections;
        using MySql.Data.MySqlClient;
        public class IEDriverTest {
            public IEDriverTest() {
            }







            public void TestGoogle() {


                object o = null;
                SHDocVw.InternetExplorer ie = new
                SHDocVw.InternetExplorerClass();
                WebBrowser wb = (WebBrowser)ie;
                wb.Visible = true;
                //Do anything else with the window here that you wish
                wb.Navigate("https://adwords.google.co.uk/um/Logout", ref o, ref o, ref o, ref o);
                while (wb.Busy) { Thread.Sleep(100); }
                HTMLDocument document = ((HTMLDocument)wb.Document);
                IHTMLElement element = document.getElementById("Email");
                HTMLInputElementClass email = (HTMLInputElementClass)element;
                email.value = "testtestingtton@gmail.com";
                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();
        }
    }
}

Try casting to an IHTMLElementCollection instead.

col = (IHTMLElementCollection)document.getElementsByTagName("textarea");

or declaring col as an IHTMLElementCollection

IHTMLElementCollection col = document.getElementsByTagName("textarea");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM