提问人: 提问时间:10/17/2008 最后编辑:codekaizen 更新时间:12/14/2018 访问量:103914
VB.NET - 单击 Webbrowser 页面上的“提交”按钮
VB.NET - Click Submit Button on Webbrowser page
问:
我在我的 webbrowser 对象上打开了一个 html 页面,我可以输入用户名和密码,但我卡住了,不知道如何提交信息。以下是用户名/密码提交的 html 代码:
<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 »"
src="/_pub/img/hp/but-login.png" />
</li>
</ul>
</fieldset>
</form>
<p>
<a href="/account/password-reset/">ACCOUNT TROUBLE?</a>
</p>
</div>
我使用以下命令输入用户名和密码:
WebBrowser1.Document.GetElementById("login-username").SetAttribute("Value", Information.txtuser.Text)
WebBrowser1.Document.GetElementById("login-password").SetAttribute("Value", Information.txtpass.Text)
我现在应该用什么来提交信息?我尝试按名称获取元素并不断获得索引超出范围错误,索引应为 -1 或 0,但确实如此。
您的帮助将不胜感激!
答:
您可以尝试为表单提供一个 ID,以便获取它,然后从 Javascript 调用调用 form.submit()。
这似乎很容易工作。
Public Function LoginAsTech(ByVal UserID As String, ByVal Pass As String) As Boolean
Dim MyDoc As New mshtml.HTMLDocument
Dim DocElements As mshtml.IHTMLElementCollection = Nothing
Dim LoginForm As mshtml.HTMLFormElement = Nothing
ASPComplete = 0
WB.Navigate(VitecLoginURI)
BrowserLoop()
MyDoc = WB.Document.DomDocument
DocElements = MyDoc.getElementsByTagName("input")
For Each i As mshtml.IHTMLElement In DocElements
Select Case i.name
Case "seLogin$UserName"
i.value = UserID
Case "seLogin$Password"
i.value = Pass
Case Else
Exit Select
End Select
frmServiceCalls.txtOut.Text &= i.name & " : " & i.value & " : " & i.type & vbCrLf
Next i
'Old Method for Clicking submit
'WB.Document.Forms("form1").InvokeMember("submit")
'Better Method to click submit
LoginForm = MyDoc.forms.item("form1")
LoginForm.item("seLogin$LoginButton").click()
ASPComplete = 0
BrowserLoop()
MyDoc= WB.Document.DomDocument
DocElements = MyDoc.getElementsByTagName("input")
For Each j As mshtml.IHTMLElement In DocElements
frmServiceCalls.txtOut.Text &= j.name & " : " & j.value & " : " & j.type & vbCrLf
Next j
frmServiceCalls.txtOut.Text &= vbCrLf & vbCrLf & WB.Url.AbsoluteUri & vbCrLf
Return 1
End Function
WebBrowser1.Document.GetElementById(*element id string*).InvokeMember("submit")
这是我对类似问题的解决方案:
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);
}
我搜索了任何解决方案,以不使用我曾经用于在浏览器中提交内容的“”方法。在这种情况下,我很高兴见到你的SendKeys(CHR(13))
InvokeMember("click")
但不知道为什么你知道你必须在那里写“点击”。 无论如何 谢谢
我 http://stackoverflow.com 受益匪浅。我徘徊在几个小时内自动登录并从 vb 应用程序提交到另一个网站。由于这个网站的帮助,我能够完成我的任务
我必须登录以下 web php 页面。
<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>
对于自动登录并单击,我写了以下 VB.Net 代码。在我放置了一个按钮和一个 Webbrowser 控件form1
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
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
只需按照两个步骤使用代码单击任意按钮即可。
聚焦要单击的按钮或元素
WebBrowser1.Document.GetElementById("place id here").Focus()
使用以下代码模拟鼠标单击
SendKeys.Send("{ENTER}")
评论