Reputation: 25
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
Upvotes: 2
Views: 284
Reputation: 18320
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 itInputElement
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")
Upvotes: 1