简体   繁体   English

如何使用C#/ IHTMLDocument2设置文本字段的值

[英]How to set value of a textfield using C# / IHTMLDocument2

I am a hobbyist programmer and want to do the following: 我是一名业余程序员,并且想要执行以下操作:

  1. Log into a site via Username - / Password 通过用户名-/密码登录站点
  2. Click an image which directs me to a certain (sub)site 单击将我定向到某个(子)站点的图像
  3. Fill out a form 填写表格
  4. BEFORE submitting the form, I want to Load the whole page and see the content of the input provided by my application before submitting it. 在提交表单之前,我想加载整个页面并在提交之前查看应用程序提供的输入内容。

I wrote several Web-scrapers / Parsers in the last few months (All in Java) but now I am facing quite some difficulties with C# and .NET. 在过去的几个月中,我写了几个Web爬虫/解析器(全部使用Java),但是现在我在使用C#和.NET时遇到了很多困难。

I am using Visual Studio 2015 IDE and I do NOT want - if possible - to use 3rd party tools / plugins etc. (if possible, please try to not provide answers hinting at HtmlAgilityPack, JSoup.. equivalents or others). 我正在使用Visual Studio 2015 IDE,并且我不希望-如果可能的话-使用第三方工具/插件等。(如果可能,请尽量不要提供暗示HtmlAgilityPack,JSoup ..等价物或其他形式的答案)。 Everything Core to .NET (or just C# in general) etc. is good though. .NET的所有核心(或一般来说只是C#)等等都是不错的。

(1) and (2) do already work, I can log in supplying username and password, and then click() on the picture and get redirected to the form using the SAME code as below. (1)和(2)已经起作用,我可以登录并提供用户名和密码,然后click()图片上的click()并使用如下所示的SAME代码重定向到表单。

I have the following Code at the moment: (CAVE: It is a WPF project ( NOT WINFORMS), using IHTMLDocument (2) 在现阶段,我有以下代码:(CAVE:这是一个WPF项目( 的WinForms),使用IHTMLDocument (2)

Currently my Code looks like the following: 目前,我的代码如下所示:

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!
    }

Every time I run the code I do the following: 每次运行代码时,我都会执行以下操作:

  1. Start application --> WebBrowser opens, directs me to the homepage. 启动应用程序-> WebBrowser打开,将我定向到主页。

  2. Click Button1 (Browser_Login) --> AutoFill username && Password --> click Submit (i am logged in now) 单击Button1(Browser_Login)->自动填充用户名&&密码->单击提交(我现在已登录)

  3. Click Button2 (Browser_ClickOnImageLinkToGetToForm) --> "Click()" on Image, get redirected to the form. 单击Image上的Button2(Browser_ClickOnImageLinkToGetToForm)->“ Click()”,重定向到该窗体。

  4. Click Button3 (Browser_FillForm) --> RunTimeException: 单击Button3(Browser_FillForm)-> RunTimeException:

Additional information: 'System.__ComObject' does not contain a definition for 'textContent' OR definition for 'value' OR definition for 'innerText' OR definition for 'InnerHtml' etc.. 附加信息:'System .__ ComObject'不包含'textContent'的定义或'value'的定义或'innerText'的定义或'InnerHtml'的定义等。

I have tried A LOT of different things, none seem to work. 我尝试了很多不同的方法,但似乎都没有效果。

The TextField i want to fill has the following properties: 我要填充的TextField具有以下属性:

    <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">

I have never encountered such problems coding in Java, also some people mention I should check for 32 / 64 bit systems and some suggested to write a Wrapper for the_COM object and some other things. 我从来没有遇到过用Java进行编码的此类问题,也有人提到我应该检查32/64位系统,还有一些人建议为the_COM对象和其他东西编写包装器。 I don't want to write a Wrapper tough, nor do i want to check for 32 -/ 64 -bit, i want to run it on every system. 我不想写一个包装器,也不想检查32位/ 64位,我想在每个系统上运行它。

Would someone provide a simple standard .Net / C# solution for this? 有人会为此提供一个简单的标准.Net / C#解决方案吗? Please keep in mind, I am a hobbyist, I am NOT a professional developer (Maybe if won't understand some super in-depth examples (I'll most definitely learn them tough)). 请记住,我是一名业余爱好者,我不是专业开发人员(也许不懂一些超级深入的示例(我肯定会很艰难地学习它们))。

TL;DR: TL; DR:

How to Fill a Form which checks content on keyup with WPF WebBrowser control using .NET / C#! 如何填写使用.NET / C#使用WPF WebBrowser控件检查注册表内容的表单!

There are a lot of things you can do if you get hold of the DOM like this: 如果掌握了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;
    }

Not sure why what you were doing wasn't working, but I copied this code from a working solution. 不知道为什么您的工作没有用,但是我从一个有效的解决方案中复制了此代码。 You can cut out the Dispatcher stuff if you are on the main thread. 如果您在主线程上,则可以剪切Dispatcher的内容。

You get a COM object that has a lot of methods just like in JavaScript. 您将获得一个具有许多方法的COM对象,就像JavaScript中一样。 So to set the text you can do something like this: 因此,要设置文本,您可以执行以下操作:

document.getElementById("bob").value = "fred";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM