【问题标题】:C# and interet explorer automationC# 和 Internet Explorer 自动化
【发布时间】:2012-04-06 11:30:36
【问题描述】:

您好,我正在开发 Internet Explorer 自动化,这对我来说是一个新领域,我收到一个错误,类型转换没有成功,你能看到我做错了什么吗?

无法将“System.__ComObject”类型的 COM 对象转换为接口类型“mshtml.HTMLElementCollection”。此操作失败,因为 IID 为“{3050F56B-98B5-11CF-BB82-00AA00BDCE0B}”的接口的 COM 组件上的 QueryInterface 调用因以下错误而失败:不支持此类接口(来自 HRESULT 的异常:0x80004002 (E_NOINTERFACE)) .

代码是:

 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();
        }
    }
}

【问题讨论】:

    标签: c# internet-explorer


    【解决方案1】:

    尝试转换为 IHTMLElementCollection

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

    或将 col 声明为 IHTMLElementCollection

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

    【讨论】:

    • Thnx,我不知道它存在。
    猜你喜欢
    • 2012-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-14
    • 2018-12-18
    • 2020-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多