简体   繁体   中英

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.

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). Everything Core to .NET (or just C# in general) etc. is good though.

(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.

I have the following Code at the moment: (CAVE: It is a WPF project ( WINFORMS), using IHTMLDocument (2) 的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.

  2. Click Button1 (Browser_Login) --> AutoFill username && Password --> click Submit (i am logged in now)

  3. Click Button2 (Browser_ClickOnImageLinkToGetToForm) --> "Click()" on Image, get redirected to the form.

  4. Click 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..

I have tried A LOT of different things, none seem to work.

The TextField i want to fill has the following properties:

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

Would someone provide a simple standard .Net / C# solution for this? 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:

How to Fill a Form which checks content on keyup with WPF WebBrowser control using .NET / C#!

There are a lot of things you can do if you get hold of the DOM like this:

    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.

You get a COM object that has a lot of methods just like in JavaScript. So to set the text you can do something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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