我的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,但它是。
你的帮助会非常适合!!
答案 0 :(得分:3)
WebBrowser1.Document.GetElementById(*element id string*).InvokeMember("submit")
答案 1 :(得分:2)
这是我对类似问题的解决方案:
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);
}
答案 2 :(得分:2)
我搜索了任何解决方案,不使用我用过的“SendKeys(CHR(13))
”方法在浏览器中提交内容。在这种情况下,我很高兴看到你的
InvokeMember("click")
但不知道为什么你知道你必须在那里写“点击”。 无论如何 感谢
答案 3 :(得分:1)
这似乎很容易。
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
答案 4 :(得分:1)
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代码。在form1
中,我放置了一个按钮和一个Web浏览器控件
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
答案 5 :(得分:0)
您可以尝试为表单提供ID,以便获取它,然后从Javascript调用中调用form.submit()。
答案 6 :(得分:0)
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
答案 7 :(得分:0)
只需按照两个步骤使用代码点击任意按钮。
聚焦您要点击的按钮或元素
WebBrowser1.Document.GetElementById("place id here").Focus()
使用以下代码模拟鼠标点击
SendKeys.Send("{ENTER}")