2

I am new to vb.net and need some help. I'm trying to autofill some fields on a website and press the send button, and I know how to do it when there is an ElementID:

WebBrowser1.Document.GetElementById("value")

But I'm stuck now on this webpage which has no ElemendIDs.

The HTML code is:

<tr>
    <td align="center">
        <br>
        <br>
        <form method="POST">
            <table class="form">
                <tr>
                    <td width="100">Login*</td>
                    <td><input type="text" name="login"></td>
                </tr>
                <tr>
                    <td>Password*</td>
                    <td><input type="text" name="password"></td>
                </tr>
                <tr>
                    <td>Full name</td>
                    <td><input type="text" name="full_name"></td>
                </tr>
                <tr>
                    <td>Account number</td>
                    <td><input type="text" name="account_number"></td>
                </tr>
                <tr>
                    <td>MAC</td>
                    <td><input type="text" name="stb_mac"></td>
                </tr>
                <tr>
                    <td>Account disabled</td>
                    <td><input type="checkbox" name="status" value="0"></td>
                </tr>
                <tr>
                    <td>Tariff plan</td>
                    <td>
                        <select name="tariff_plan_id">
                            <option value="0">---</option>
                            <option value="1">Full</option>                            </select>
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="submit" value="Add"/></td>
                </tr>
            </table>
        </form>
    </td>
</tr>

How do I find the right values to write inside these fields? Also, how can I select <option value="1">Full</option> and then press the button to send the autofilled values to site?

Thank you for your help


EDIT:

@VisualVincent I tried your third example and managed to find:

<table class="form">

I'm now stuck on how to find this child:

<td><input type="text" name="login"></td>

This is what I tried:

If Element.GetAttribute("className") = "form" Then
    Console.WriteLine("found form")
    'Parent found.
    'Inner loop, looking for the child element we want (<option value="5">Banana</option>).
    For Each OptionElement As HtmlElement In Element.GetElementsByTagName("td")
        If OptionElement.GetAttribute("name") = "login" Then
            'Found. Do something with 'OptionElement'...
            Console.WriteLine("login found")
            ElementFound = True
            Exit For 'Exit the inner loop.
        End If
    Next
End If

I also tried to find the other elements, for instance:

For Each OptionElement As HtmlElement In Element.GetElementsByTagName("input")
    If OptionElement.GetAttribute("name") = "stb_mac" Then
        Element.SetAttribute("value", variable(4))

        ElementFound = True
        Exit For 'Exit the inner loop.
    End If
Next

And for some reason that doesn't work, but this does:

For Each Element As HtmlElement In WebBrowser1.Document.All 'Iterate all <select> tags. You can use Document.All here as well.
    If Element.GetAttribute("name") = "login" Then Element.SetAttribute("value", variable(0))
    If Element.GetAttribute("name") = "password" Then Element.SetAttribute("value", variable(1))
    If Element.GetAttribute("name") = "full_name" Then Element.SetAttribute("value", variable(2))
    If Element.GetAttribute("name") = "account_number" Then Element.SetAttribute("value", variable(3))
    If Element.GetAttribute("name") = "stb_mac" Then Element.SetAttribute("value", variable(4))
Next
Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
morgalis
  • 25
  • 4
  • See my answer to the following question: Possible duplicate of [Is there a possibility to address elements on a website which have no ID?](https://stackoverflow.com/a/48816114/3740093) – Visual Vincent Sep 25 '19 at 11:20
  • thanks for the reply but with the link you send me i don't understand how to find them and how to write inside the field also i dont know how to press the add button iam still to new in vbnet – morgalis Sep 25 '19 at 13:56
  • What part(s) don't you understand and what attempts did you make? You must of course understand the terminology involved and the basics of the HTML structure in order to be able to accomplish anything. My answer includes both explanations and complete example code (with comments) showing various ways to locate a specific element depending on your needs and what information the element contains. I haven't covered how to click an element, but that is only one line of code which you can find via a simple Google search. – Visual Vincent Sep 25 '19 at 14:10
  • the click i found via google WebBrowser1.Document.Forms(0).InvokeMember("submit") i dont understand how to find this field Login* and enter a string inside – morgalis Sep 25 '19 at 14:28
  • That code submits the first form on the page, it doesn't actually invoke a click (though that shouldn't matter as long as the button doesn't have any other event handlers attached to it). `InvokeMember("submit")` is what does the actual submission. – Visual Vincent Sep 25 '19 at 14:36
  • The first and third example in my answer should help you find any element you need from that HTML. The first, specifically, shows how you can locate an element based on one of its attributes (in that case the `class` attribute). -- Once located, changing an `input` element's contents (text) is then just a matter of setting its `value` attribute. – Visual Vincent Sep 25 '19 at 14:41
  • found now how to set the combobox to 1 with For Each OptionElement As HtmlElement In Element.GetElementsByTagName("select") If OptionElement.GetAttribute("value") = "0" Then 'Found. Do something with 'OptionElement'... OptionElement.SetAttribute("value", 1) – morgalis Sep 25 '19 at 17:42
  • sorry dident know this – morgalis Sep 25 '19 at 18:24
  • No worries. You're not the first to do so :). Also, credit to you for actually making an effort and even managing to solve parts of the problem yourself. It often happens that new users expect us to do the work for them or that they are/seem unwilling to make any attempt at all, but you've done what many others never do. Good job! – Visual Vincent Sep 25 '19 at 18:31
  • If you have any more questions I can answer them in my existing answer as well (or in the comments, if they're short), as long as they do not stray too far from your original question. – Visual Vincent Sep 25 '19 at 18:44
  • Hmm, in response to your update: So you're saying that first finding the form and _then_ finding `stb_mac` doesn't work, but looking for `stb_mac` directly from `Document.All` does? If so, that could mean that there's another element with `class="form"` that your code found first. You might want to narrow the first loop down (`For Each Element ...`) by only iterating `GetElementsByTagName("form")`. -- Either way there's nothing wrong with iterating `Document.All` either. There will likely only be one instance of each `name` if there is only one form on the page. – Visual Vincent Sep 25 '19 at 19:03
  • @Visual Vincent i have 1 more question when i use the send button is there chance to see what values the button send ? so i can send all infos without insert in the webmask – morgalis Oct 04 '19 at 12:40
  • You mean you want to see everything that is sent to the server when you click the button that submits the form? Sure, there's a built-in feature in Google Chrome's developer tools for that: https://stackoverflow.com/a/15603882 (note: instead of refreshing the page in step 3, you press the button to submit the form) – Visual Vincent Oct 04 '19 at 12:49
  • i will try this thank you again for help – morgalis Oct 04 '19 at 20:53
  • new day new problem i try to call function genre_proc(num){ with WebBrowser1.Document.InvokeScript("genre_proc(1)") but dont work i know now with help of Visual Vincent how to change and find the combobox and set values but this function need to get to next step this invoke.Script work for me when there is a function like function check_protocol(){ anyone can help me thank you – morgalis Oct 05 '19 at 23:58
  • You need to check the [documentation](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.htmldocument.invokescript?view=netframework-4.8#System_Windows_Forms_HtmlDocument_InvokeScript_System_String_System_Object___). You're only supposed to specify the name of the script function, then you pass its parameters (if any) as a second argument: `InvokeScript("genre_proc", New Object() {1})` -- To pass multiple arguments use: `New Object() {arg1, arg2, arg3, ...}` – Visual Vincent Oct 06 '19 at 06:15
  • @Visual Vincent thank you so mutch – morgalis Oct 06 '19 at 11:17

1 Answers1

1

You're almost there. The only problem is here:

For Each OptionElement As HtmlElement In Element.GetElementsByTagName("td")
    If OptionElement.GetAttribute("name") = "login" Then

You're simply iterating the wrong element type ("tag"). You're iterating <td> elements, whereas name="login" is applied to an <input> element.

Change the loop to the following, and it should work:

For Each OptionElement As HtmlElement In Element.GetElementsByTagName("input")

As a side note I also recommend you change the variable name to something which better describes what it actually is. OptionElement was used in my example because I iterate <option> elements, so in this case I'd name it InputElement instead:

For Each InputElement As HtmlElement In Element.GetElementsByTagName("input")

Then to programmatically put text in the input box/text box, simply use the SetAttribute() method to modify the value attribute:

InputElement.SetAttribute("value", "someTextHere")
Visual Vincent
  • 18,045
  • 5
  • 28
  • 75