显示“对象”的WPF不包含“GetElementById”的定义

时间:2014-05-08 17:32:49

标签: c# wpf getelementbyid

我的WPF应用程序的一部分有两个用于用户名和密码的文本框。只需单击一个按钮,程序就会将用户登录到一个网站。问题是GetElementById引发了一个错误:

  

'对象'不包含' GetElementById'的定义和不   扩展方法' GetElementById'接受第一个类型的参数   '对象'可以找到(你是否错过了使用指令或   装配参考?)

有问题的代码是:

    private void webBrowser1_LoadCompleted(object sender, NavigationEventArgs e)
    {
            webBrowser1.Document.GetElementById("AccountBarLogin").InvokeMember("click");
            webBrowser1.Document.GetElementById("QuickLoginEmail").SetAttribute("value", textBox1.Text);
            webBrowser1.Document.GetElementById("QuickLoginPassword").SetAttribute("value", textBox2.Text);
            webBrowser1.Document.GetElementById("QuickLoginSubmit").InvokeMember("click");
    }

我添加了对System.Windows.Forms以及MSHTML的引用。究竟需要引用什么来防止GetElementByID抛出此错误?

看到这个:System.Windows.Forms.HtmlDocument does not contain a definition for GetElementByID但在这种情况下它似乎不是一个错字。

1 个答案:

答案 0 :(得分:-1)

这在WPF WebBrowser控件中不可用。

您将需要使用WinForm WebBrowser控件并将其添加到WindowsFormHosted控件。

将WindowsFormHosted添加到您的页面,然后在后面的代码中创建WinForm WebBrowser控件。

XAML

xmlns:my="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIn‌​tegration"

背后的代码

System.Windows.Forms.WebBrowser browser = new System.Windows.Forms.WebBrowser();
windowsFormsHost.Child = browser;