Mike R. Mike R. - 6 months ago 28
Vb.net Question

How to get button using VB.NET

I am not sure how to invoke a button click using VB.NET:

<form id="form-login" style="clear: both;" name="login" method="post" action="index.php">
<p id="form-login-username">
<label for="modlgn_username">Username</label>
<input id="modlgn_username" class="inputbox" type="text" size="15" name="username">
</p>
<p id="form-login-password">
<p id="form-login-lang" style="clear: both;">
<div class="button_holder">
<div class="button1">
<div class="next">
<a onclick="login.submit();"> Login</a>
</div>
</div>
</div>
<div class="clr"></div>
<input type="submit" value="Login" style="border: 0; padding: 0; margin: 0; width: 0px; height: 0px;">
<input type="hidden" value="com_login" name="option">
<input type="hidden" value="login" name="task">
<input type="hidden" value="1" name="19c1f84e789e8862fce26aa45d57a980">
</form>


What I am trying to do is use this line of code to invoke a button click once the webpage is fully loaded. Everything works except the click, I am not sure how to get the button.

WebBrowser1.Document.GetElementsByTagName(" ... ").InvokeMember("click")


I tried a bunch of stuff for ... but none of it works. Would really appreciate any feedback.

THE SOLUTION:

Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
If Me.WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
WebBrowser1.Document.GetElementById("username").SetAttribute("value", "admin")
WebBrowser1.Document.GetElementById("passwd").SetAttribute("value", "...")
WebBrowser1.Document.GetElementById("form-login").InvokeMember("submit")
End If
End Sub

Answer

It might be easier to submit the form instead.

Dim form As HtmlElement = WebBrowser1.Document.GetElementById("form-login")

If form IsNot Nothing Then
        form.InvokeMember("submit")
End If