VB.NET - Click Submit Button on Webbrowser page

前端 未结 8 623
时光说笑
时光说笑 2021-02-09 02:30

I have a html page open on my webbrowser object, I can enter username and password okay, but I\'m stuck and don\'t know how to submit the info. Here is the html code for the us

相关标签:
8条回答
  • 2021-02-09 02:37

    I searched for any solution to not use the "SendKeys(CHR(13))" methode I ever used to submit stuff in Browser. In this case I was happy to see your

    InvokeMember("click")
    

    but dont know why you know that you have to write "click" in there. Anyway Thanks

    0 讨论(0)
  • 2021-02-09 02:38

    You could try giving an ID to the form, in order to get ahold of it, and then call form.submit() from a Javascript call.

    0 讨论(0)
  • 2021-02-09 02:50

    Just follow two steps for clicking a any button using code.

    1. focus the button or element which you want to click

      WebBrowser1.Document.GetElementById("place id here").Focus()

    2. simulate mouse click using this following code

      SendKeys.Send("{ENTER}")

    0 讨论(0)
  • 2021-02-09 02:57

    This is my solution for something similar to this problem:

    System.Windows.Forms.WebBrowser www;
    
    void VerificarWebSites()
    {
        www = new System.Windows.Forms.WebBrowser();
        www.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(www_DocumentCompleted_login);
        www.Navigate(new Uri("http://www.meusite.com.br"));
    }
    
    void www_DocumentCompleted_login(object sender, WebBrowserDocumentCompletedEventArgs e)
    {            
        www.DocumentCompleted -= new WebBrowserDocumentCompletedEventHandler(www_DocumentCompleted_login);
        www.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(www_DocumentCompleted_logado);
    
        www.Document.Forms[0].All["tbx_login"].SetAttribute("value", "Gostoso");
        www.Document.Forms[0].All["tbx_senha"].SetAttribute("value", "abcdef");
        www.Document.GetElementById("btn_login").Focus();
        www.Document.GetElementById("btn_login").InvokeMember("click");
    }
    
    void www_DocumentCompleted_logado(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        System.IO.StreamWriter sw = new StreamWriter("c:\\saida_teste.txt");
        sw.Write(www.DocumentText);
        sw.Close();
        MessageBox.Show(e.Url.AbsolutePath);
    }
    
    0 讨论(0)
  • 2021-02-09 02:59
      Private Sub bt_continue_Click(sender As Object, e As EventArgs) Handles bt_continue.Click
        wb_apple.Document.GetElementById("phoneNumber").Focus()
        wb_apple.Document.GetElementById("phoneNumber").InnerText = tb_phonenumber.Text
        wb_apple.Document.GetElementById("reservationCode").Focus()
        wb_apple.Document.GetElementById("reservationCode").InnerText = tb_regcode.Text
        'SendKeys.Send("{Tab}{Tab}{Tab}")
        'For Each Element As HtmlElement In wb_apple.Document.GetElementsByTagName("a")
        'If Element.OuterHtml.Contains("iReserve.sms.submitButtonLabel") Then
        'Element.InvokeMember("click")
        'Exit For
        ' End If
        'Next Element
        wb_apple.Document.GetElementById("smsPageForm").Focus()
        wb_apple.Document.GetElementById("smsPageForm").InvokeMember("submit")
    
    End Sub
    
    0 讨论(0)
  • 2021-02-09 03:00

    I am quite benefited with http://stackoverflow.com. I was wandering from hours for automatic login and submit from vb application to another web site. Due to help of this site I am able to complete my task

    I have to login following web php page.

    <HTML>
    
    <body>
    <div align="center"><img src="banner.png" height="80px" /></div>
    <script type="text/javascript">
    $(document).ready(function(){
                $("#login").validate();
                $("#login_container").css({'position': 'absolute', 
                    'top' : (($(window).height()/2) - $("#login_container").height()/2)+'px'});
                $("#login_container").css({'left' : (($(window).width()/2) - $("#login_container").width()/2)+'px'});
            });
        </script>
        <div id="login_container">
            <form name="login" id="login" action="?q=login" method="post">
            <table>
              <tr><td>Username</td><td><input type="text" name="name" class="required"/></td></tr>
              <tr><td>Password</td><td><input type="password" name="password" class="required"/></td></tr>
              <tr><td></td><td><input type="submit" name="subimt" value="Login" /></td></tr>
            </table>
            </form>
        </div>
    </body>
    </html>
    

    For automatic Login and clicking I wrote following VB.Net Code. In form1 I placed a button and a Webbrowser control

    Imports System.IO
    Imports System.Windows.Forms
    
    
    
    Public Class Form1
    
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
    
            WebBrowser1.Navigate("http://xyz.com")
    
    
    
        End Sub
    
        Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
            WebBrowser1.Document.GetElementById("name").SetAttribute("Value", "bharatlal")
            WebBrowser1.Document.GetElementById("password").SetAttribute("Value", "mahato")
            WebBrowser1.Document.GetElementById("subimt").Focus()
            WebBrowser1.Document.GetElementById("subimt").InvokeMember("click")
        End Sub
    End Class
    
    0 讨论(0)
提交回复
热议问题