如何使用C#/IHTMLDocument2设置文本字段的值
我是一名程序员爱好者,想做以下工作:如何使用C#/IHTMLDocument2设置文本字段的值,c#,.net,wpf,com,ihtmldocument2,C#,.net,Wpf,Com,Ihtmldocument2,我是一名程序员爱好者,想做以下工作: 通过用户名/密码登录到站点 单击将我引导到某个(子)站点的图像 填表 在提交表单之前,我希望加载整个页面,并在提交之前查看应用程序提供的输入内容 在过去的几个月里,我写了几个Web scraper/parser(都是用Java编写的),但现在我在使用C#和.NET时遇到了不少困难 我使用的是Visual Studio 2015 IDE,如果可能的话,我不想使用第三方工具/插件等(如果可能的话,请尽量不要在HtmlAgilityPack、JSoup..或其他类
IHTMLDocument
(2)
当前我的代码如下所示:
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Navigation;
using mshtml;
using System.Diagnostics;
public partial class MainWindow : Window
{
private CustomObject testObject;
public IHTMLDocument2 doc2;
public MainWindow()
{
InitializeComponent();
Browser.Navigate("https://www.xXx.xXx/"); //The Browser is the standard WPF WebBrowser
// Several other functions like LoadComplete etc. here
}
// Function for login
private void Browser_Login(object sender, RoutedEventArgs e)
{
doc2.all.item("ID_OF_USERNAME_TEXTFIELD").value = "MyUsername";
doc2.all.item("ID_OF_PASSWORD_TEXTFIELD").value = "MyPassWord";
doc2.all.item("NAME_OF_SUBMIT_BUTTON").click();
}
private void Browser_ClickOnImageLinkToGetToForm(object sender, RoutedEventArgs e)
{
// Logic to get to the Form, everything works as expected
}
private void Browser_FillForm(object sender, RoutedEventArgs e)
{
doc2.all.item("NAME_OF_THE_TEXTFIELD_TO_FILL").value = "Text if want to put into the field";
// /repeat for all other TextFields and a couple of other input elements.
--> Exception!
}
每次运行代码时,我都会执行以下操作:
<input class="TxtField1" maxlength="800" type="text" id="Title" name="Title" value="" onkeyup="checkField(this.name);" onblur="checkField(this.name);" style="width: 550px; cursor: help; background-color: rgb(228, 234, 224);" title="Some Title">
我从未在Java编码中遇到过这样的问题,也有人提到我应该检查32/64位系统,有人建议为_COM对象和其他东西编写一个包装器。我不想编写一个很难的包装器,也不想检查32/64位,我想在每个系统上运行它
有人能提供一个简单的标准.Net/C解决方案吗?请记住,我是一个业余爱好者,我不是一个专业的开发人员(如果我不理解一些超级深入的例子(我肯定会很难理解)
TL;DR:
如何使用.NET/C#!使用WPF WebBrowser控件填写一个检查键控内容的表单如果您像这样掌握DOM,您可以做很多事情:
private dynamic GetDOM(WebBrowser wb)
{
dynamic document = null;
System.Threading.Thread.Sleep(500);
while (document == null)
{
Dispatcher.Invoke(() => document = wb.Document);
System.Threading.Thread.Sleep(100);
}
return document;
}
document.getElementById("bob").value = "fred";
不确定为什么你所做的不起作用,但我从一个有效的解决方案中复制了这段代码。如果你在主线程上,你可以删除Dispatcher的内容
您会得到一个COM对象,它有很多方法,就像JavaScript中一样。因此,要设置文本,您可以执行以下操作:
private dynamic GetDOM(WebBrowser wb)
{
dynamic document = null;
System.Threading.Thread.Sleep(500);
while (document == null)
{
Dispatcher.Invoke(() => document = wb.Document);
System.Threading.Thread.Sleep(100);
}
return document;
}
document.getElementById("bob").value = "fred";
如果像这样掌握DOM,您可以做很多事情:
private dynamic GetDOM(WebBrowser wb)
{
dynamic document = null;
System.Threading.Thread.Sleep(500);
while (document == null)
{
Dispatcher.Invoke(() => document = wb.Document);
System.Threading.Thread.Sleep(100);
}
return document;
}
document.getElementById("bob").value = "fred";
不确定为什么你所做的不起作用,但我从一个有效的解决方案中复制了这段代码。如果你在主线程上,你可以删除Dispatcher的内容
您会得到一个COM对象,它有很多方法,就像JavaScript中一样。因此,要设置文本,您可以执行以下操作:
private dynamic GetDOM(WebBrowser wb)
{
dynamic document = null;
System.Threading.Thread.Sleep(500);
while (document == null)
{
Dispatcher.Invoke(() => document = wb.Document);
System.Threading.Thread.Sleep(100);
}
return document;
}
document.getElementById("bob").value = "fred";