【问题标题】:How to set value of a textfield using C# / IHTMLDocument2如何使用 C#/IHTMLDocument2 设置文本字段的值
【发布时间】:2016-05-11 18:19:00
【问题描述】:

我是一名业余程序员,想做以下事情:

  1. 通过用户名-/密码登录网站
  2. 单击将我定向到某个(子)站点的图像
  3. 填写表格
  4. 在提交表单之前,我想加载整个页面并在提交之前查看我的应用程序提供的输入内容。

在过去的几个月里,我编写了几个 Web-scrapers / Parsers(全部使用 Java),但现在我在使用 C# 和 .NET 时遇到了一些困难。

我正在使用 Visual Studio 2015 IDE,并且我不希望 - 如果可能的话 - 使用 3rd 方工具/插件等(如果可能,请尽量不要提供暗示 HtmlAgilityPack、JSoup.. 等价物或其他的答案)。不过,从 Core 到 .NET(或一般来说只是 C#)等的一切都很好。

(1) 和 (2) 已经工作了,我可以提供用户名和密码登录,然后在图片上click() 并使用以下相同的代码重定向到表单。

我目前有以下代码:(CAVE:这是一个 WPF 项目(NOT WINFORMS),使用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!
    }

每次运行代码时,我都会执行以下操作:

  1. 启动应用程序 --> WebBrowser 打开,将我引导至主页。

  2. 点击 Button1 (Browser_Login) --> AutoFill username && Password --> 点击提交(我现在登录了)

  3. 单击 Button2 (Browser_ClickOnImageLinkToGetToForm) --> Image 上的“Click()”,重定向到表单。

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

附加信息:“System.__ComObject”不包含“textContent”的定义或“value”的定义或“innerText”的定义或“InnerHtml”的定义等。

我尝试了很多不同的方法,但似乎都不起作用。

我要填充的 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">

我在 Java 编码中从未遇到过这样的问题,也有人提到我应该检查 32 / 64 位系统,有些人建议为 the_COM 对象和其他一些东西编写一个 Wrapper。我不想写一个很难的 Wrapper,也不想检查 32 位/64 位,我想在每个系统上运行它。

有人会为此提供一个简单的标准 .Net / C# 解决方案吗?请记住,我是一个业余爱好者,我不是一个专业的开发者(也许如果不理解一些超级深入的例子(我肯定会努力学习它们))。

TL;DR:

如何使用 .NET / C# 填写一个使用 WPF WebBrowser 控件检查 keyup 内容的表单!

【问题讨论】:

    标签: c# .net wpf com ihtmldocument2


    【解决方案1】:

    如果你像这样掌握 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;
        }
    

    不确定为什么您的操作不起作用,但我从一个有效的解决方案中复制了这段代码。如果你在主线程上,你可以剪掉 Dispatcher 的东西。

    你会得到一个 COM 对象,它有很多方法,就像在 JavaScript 中一样。因此,要设置文本,您可以执行以下操作:

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

    【讨论】:

      猜你喜欢
      • 2020-06-03
      • 2021-07-23
      • 2015-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-20
      • 1970-01-01
      相关资源
      最近更新 更多