vb 点击
引用:http://www.lob.cn/jq/skill/2980.shtml
可用于网页自动登录、网页模拟点击,调用到公共控件WebBrowser,模拟表单的填写及鼠标点击事件等,演示的代码部分如下:
判断是否网络正常
Private Function IsConnectedToInternet() As Boolean Dim Desc As Integer Return internet.InternetGetConnectedState(Desc, 0) End Function
设置要访问的页面:
webBrowser1.Navigate(New Uri(http://www.lob.cn))
Private Sub btnlogin_Click(sender As Object, e As EventArgs) webBrowser1.Document.GetElementById("id").InnerText = txtuser.Text.ToString().Trim() '填写登录用户名 webBrowser1.Document.GetElementById("passwd").InnerText = txtpwd.Text.ToString().Trim() '填写登录密码
Dim formLogin As HtmlElement = webBrowser1.Document.Forms("frmLogin") formLogin.InvokeMember("submit") Thread.Sleep(5000) herfclick(http://www.lob.cn) '这是登录成功的操作 '注意不是直接跳过去的,模拟点击链接 'SESSION不会丢失 herfclick(http://www.lob.cn) End Sub
模拟点击url链接:
Private Sub herfclick(url As String) For i As Integer = 0 To webBrowser1.Document.All.Count - 1 If webBrowser1.Document.All(i).TagName = "A" AndAlso webBrowser1.Document.All(i).GetAttribute("href").ToString().Trim() = url Then webBrowser1.Document.All(i).InvokeMember("click") '引发”CLICK”事件 更多的.net源代码来自乐博网 lob.cn Exit For End If Next End Sub
填充数据:
Private Sub filldate(txt As String) Dim frmWord As HtmlElement = webBrowser1.Document.Forms("frmWord") '找到from Dim txtarea As HtmlElementCollection = frmWord.GetElementsByTagName("TEXTAREA") Dim radio As HtmlElementCollection = frmWord.GetElementsByTagName("INPUT") radio(2).InvokeMember("click") '选择radio控件 txtarea(0).InnerText = txt.ToString() End Sub
原理很简单,在webBrowser1的DocumentCompleted事件中判断一下当前完成的url地址,如果当前完成url和当初要打开的url相等,
就确定当前网页已经加载完成
'string strURL 要找开的页面的URL Private Sub webBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) '已经加载完成 If e.Url.ToString().Trim() = strURL.Trim() Then End If End Sub
'控制流动条 滚动到你要的坐标,以像素为单位; webBrowser1.Document.Window.ScrollTo(100, 100)