嗨,我正在尝试使用 c# 单击按钮,但我不断收到转换错误
Unable to cast COM object of type 'mshtml.HTMLInputElementClass' to class type 'mshtml.HTMLButtonElementClass'
. 表示 COM 组件的类型的实例不能转换为表示 COM 组件的不同类型;但是,只要底层 COM 组件支持对接口的 IID 的 QueryInterface 调用,它们就可以转换为接口。
我究竟做错了什么?代码如下:
namespace IEAutomation {
/// <summary>
/// Summary description for IEDriverTest.
/// </summary>
///
using mshtml;
using System.Threading;
using SHDocVw;
public class IEDriverTest {
public IEDriverTest() {
}
public void TestGoogle() {
object o = null;
SHDocVw.InternetExplorer ie = new
SHDocVw.InternetExplorerClass();
IWebBrowserApp wb = (IWebBrowserApp)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");
HTMLButtonElementClass subm = (HTMLButtonElementClass)element;//ERROR HERE
subm.click();
}
}
}