2012-04-06 17 views
1

こんにちは、私はインターネットエクスプローラの自動化に取り組んでいます。これは私にとって新しい領域です。タイプメイドでエラーが発生しましたが、成功しませんでした。C#とinteretエクスプローラの自動化

タイプ '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 = "[email protected]"; 
       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(); 
     } 
    } 
} 

答えて

1

ではなくIHTMLElementCollectionにキャストしてみます。 IHTMLElementCollection

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

のthnxとして

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

または宣言COL、私はそれが存在していた知りませんでした。 – Evan