2012-04-06 67 views
1

嗨,我正在研究一個Internet Explorer自動化,這是我的新領域,我得到一個錯誤,沒有成功的類型轉換可以看到我做錯了什麼?C#和interet資源管理器自動化

無法將'System .__ ComObject'類型的COM對象轉換爲接口類型'mshtml.HTMLElementCollection'。此操作失敗,因爲IIC「{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代替。

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

或聲明山坳作爲IHTMLElementCollection

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

日Thnx,我不知道存在。 – Evan 2012-04-06 11:12:23