VB.NET - Click Submit Button on Webbrowser page

2019-03-27 10:34发布

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 username/password submit:

<div id="signin">
    <h2 class="ir">
        <em></em>Sign in</h2>
    <form action="/login/" method="post">
    <input id="login-url" name="login[url]" 
           type="hidden" value="/characters/" />
    <input id="login-urlError" name="login[urlError]" 
           type="hidden" value="/account/?error=1" />
    <fieldset>
        <ul>
            <li class="row">
                <label for="login-username">
                    Username <span class="req">*</span>
                </label>
                <input id="login-username" name="login[username]"
                        type="text" class="TextBox" value="" />
            </li>
            <li class="row">
                <label for="login-password">
                    Password <span class="req">*</span>
                </label>
                <input id="login-password" name="login[password]"
                       type="password" class="TextBox Password" value="" />
            </li>
            <li class="but">
                <input name="login[submit]" type="image" 
                       class="img" alt="Login &raquo;" 
                       src="/_pub/img/hp/but-login.png" />
            </li>
        </ul>
    </fieldset>
    </form>
    <p>
        <a href="/account/password-reset/">ACCOUNT TROUBLE?</a>
    </p>
</div>

I use the following to enter the username and password:

WebBrowser1.Document.GetElementById("login-username").SetAttribute("Value", Information.txtuser.Text)
WebBrowser1.Document.GetElementById("login-password").SetAttribute("Value", Information.txtpass.Text)

What should I use to submit the info now? I tried getting the element by name and kept getting index out of range error, index should be -1 or 0, but it was.

Your help would be greatly appriecated!!

8条回答
女痞
2楼-- · 2019-03-27 11:01

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}")

查看更多
ら.Afraid
3楼-- · 2019-03-27 11:02

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);
}
查看更多
登录 后发表回答