VB.NET - 单击 Web 浏览器页面上的提交按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/210342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB.NET - Click Submit Button on Webbrowser page
提问by
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:
我在我的 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>
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.
我现在应该用什么来提交信息?我尝试按名称获取元素并不断获取索引超出范围错误,索引应为 -1 或 0,但确实如此。
Your help would be greatly appriecated!!
您的帮助将不胜感激!!
回答by
WebBrowser1.Document.GetElementById(*element id string*).InvokeMember("submit")
WebBrowser1.Document.GetElementById(*element id string*).InvokeMember("submit")
回答by Rafael Pileggi
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);
}
回答by Nasenbaer
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
我搜索了任何不使用SendKeys(CHR(13))
我曾经在浏览器中提交内容的“ ”方法的解决方案。在这种情况下,我很高兴看到你
InvokeMember("click")
but dont know why you know that you have to write "click" in there. Anyway Thanks
但不知道为什么你知道你必须在那里写“点击”。不管怎样,谢谢
回答by Kushawaha Bharat
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
我非常受益于http://stackoverflow.com。我在几个小时内徘徊以进行自动登录并从 vb 应用程序提交到另一个网站。由于本网站的帮助,我能够完成我的任务
I have to login following web php page.
我必须登录以下 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>
For automatic Login and clicking I wrote following VB.Net Code. In form1
I placed a button and a Webbrowser control
对于自动登录和单击,我编写了以下 VB.Net 代码。在form1
我放置了一个按钮和一个 Webbrowser 控件
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
回答by Kushawaha Bharat
This seems to work easily.
这似乎很容易工作。
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
回答by cdeszaq
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.
您可以尝试为表单提供一个 ID,以便获得它,然后从 Javascript 调用中调用 form.submit()。
回答by user2941395
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
回答by Mohammad Ziya ul haq
Just follow two steps for clicking a any button using code.
只需按照两个步骤使用代码单击任意按钮即可。
focus the button or element which you want to click
WebBrowser1.Document.GetElementById("place id here").Focus()
simulate mouse click using this following code
SendKeys.Send("{ENTER}")
聚焦要单击的按钮或元素
WebBrowser1.Document.GetElementById("place id here").Focus()
使用以下代码模拟鼠标点击
SendKeys.Send("{ENTER}")